How to use EventInfo.Addhandler to bind a local EventHandler

Jun 4 2014 6:47 AM
 Hi,

I am trying to bind an eventHandler to an event raised from an in-memory compiled source code. Below is the detail.

 //======================
// Name of the saved file ABC.cs
using CustomDll; <- A compiled library containg Events and Some functions

class ABC
{
//ReadOnlyEventArgs defined in CustomDll
  public event EventHandler<ReadOnlyEventArgs<int>> OnSubscription;
    public  event EventHandler<ReadOnlyEventArgs<int>> OnUnSubscription;
 
 int CMETOKEN = 178569; // Can be anything Say Electronic Tradable token of Silver
 
 private void Subscribe(int Token) { OnSubscription.Raise(OnSubscription, OnSubscription.CreateReadOnlyArgs(Token)); }
    private void UnSubscribe(int Token) { OnUnSubscription.Raise(OnUnSubscription, OnUnSubscription.CreateReadOnlyArgs(Token)); }
 
 ABC()
 {
  Subscribe(CMETOKEN);
 }
 ~ABC()
 {
  UnSubscribe(CMETOKEN);
 }
 
 other methods and functions defined here ....
 
}
//======================
Note : This class compiles without any error
Now the rule to run as automated Trade System is ready.
I use CodeDomProvider to compile this code in memory and then need to connect the events. Here I am facing the real problem. I am unable to bind any of
 the above event with an event handler in the form where the code is compiled.
 
 Form1.vb
 
 //Note : There is no logic of using vb here as i had this compiler written earlier so thought to use it with some changes.
 
  Public Sub OnDataArrived(ByVal o As Object, ByVal Stat As ReadOnlyEventArgs(Of Integer)) // I want the event raised from ABC.dll to be handled here
  End Sub
 
 //inside button click event
 
                Dim Provider As CodeDomProvider = Nothing
                Provider = CodeDomProvider.CreateProvider("CSharp")
                Dim CVB As System.CodeDom.Compiler.ICodeCompiler
                CVB = Provider.CreateCompiler
                Dim PM As New System.CodeDom.Compiler.CompilerParameters
                PM.GenerateInMemory = True
              
                PM.OutputAssembly = "ABC.dll"
                PM.MainClass = "ABC"
                PM.IncludeDebugInformation = True
                If CounterValue <= 0 Then
                    Dim ASM As System.Reflection.Assembly
                    For Each ASM In AppDomain.CurrentDomain.GetAssemblies()
                        PM.ReferencedAssemblies.Add(ASM.Location)
                    Next
                End If
                PM.ReferencedAssemblies.Add(Application.StartupPath + Path.DirectorySeparatorChar + "CustomDll.dll")
                'Get compilation results
                Dim Results As System.CodeDom.Compiler.CompilerResults
                Results = CVB.CompileAssemblyFromSource(PM, tbcode.Text)
                'Show possible compilation errors
                Dim Err As System.CodeDom.Compiler.CompilerError
                For Each Err In Results.Errors
                    ListBox1.Items.Add("Error N. " & Err.ErrorNumber & " Message: " & Err.ErrorText & " Line " & Err.Line)
                Next
                'Use the compiled assembly
                If (ListBox1.Items.Count = 0) Then
                    RunObj = New Object
                    RunObj = Results.CompiledAssembly.CreateInstance(
                      "OrderStg", False,
                      Reflection.BindingFlags.CreateInstance, Nothing,
                      {}, Nothing, Nothing)

                    Dim members As MemberInfo() = RunObj.GetType().GetMembers(BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.Static)
                    indent += 1
                    For Each M As MemberInfo In members
                        If (M.MemberType = MemberTypes.Method) Then
                            For Each pi As ParameterInfo In (CType(M, MethodInfo)).GetParameters()
                                Display(indent + 1, "Parameter: Type={0}, Name={1}", pi.ParameterType, pi.Name)
                            Next pi
                        End If
                        If (M.MemberType = MemberTypes.Event) Then
                            Dim Evnt As EventInfo = CType(M, EventInfo)
                          
                            If (Evnt.Name = "OnSubscription") Then
                                Dim events As Type = Evnt.EventHandlerType  //Gets desired Value here
                                Dim InvokeMethod As MethodInfo = events.GetMethod("Invoke")//Gets desired Value here
                                Dim Param() As ParameterInfo = InvokeMethod.GetParameters()//Gets desired Value here
        
        
                                messageTarget = Function(i As Integer) OnDataArrived(i) //Trial 1 : Error 2 Argument not specified for parameter 'Stat' of 'Public Sub OnDataArrived(o As Object, Stat As AutoTrader.ReadOnlyEventArgs(Of Integer))'.
                                Dim D As [Delegate] = [Delegate].CreateDelegate(events, OnDataArrived) //Trial 2 : Error 3 Argument not specified for parameter ........

                                'Evnt.AddEventHandler(Evnt, D)
                            End If
                        End If
                    Next

                End If
 
  // button click event ends
 
 
  Please help me out..
 
Error Snapshot:
 
Regards
Shailendra Kumar