title: Event handling of an loaded assembly at run-time
body: <p>Here is the scenario that I am trying to learn from which:</p>
  
  <ul>
  <li>There is DLL file raising an event sometimes. </li>
  <li>The DLL file cannot be added to references in source code, but it is available on disk, and the program can reach it at run-time. </li>
  <li>I loaded DLL files as an assembly for runtime. </li>
  <li>I am trying to subscribe to the event from the DLL (I know the signature and parameter formats), and handle them in my program.</li>
  </ul>
  
  <p>The Assembly is a simple Dll with a method that adds its two arguments and raises an event with a custom argument including the result of the sum operation. Here is the the code for the DLL:</p>
  
  <pre><code>namespace Dll1
  {
      public class Class1
      {
          public int c = 0;
  
          public void add(int a, int b)
          {
              c =  a + b;
              if (Added !=null)
                  Added(this, new AddArgs(c));
          }
  
          public delegate void AddHandler(object sender, AddArgs e);
  
          public event AddHandler Added;
  
      }
  
      public class AddArgs : EventArgs
      {
          private int intResult;
  
          public AddArgs(int _Value) 
          {
              intResult = _Value;
          }
  
          public int Result
          {
              get { return intResult; }
          }
      }
  }
  </code></pre>
  
  <p>Then, in my program I loaded that DLL using Assembly.LoadFile. I have another class in my program called <em>EventProcessor</em> that contains an event handler to process events from the loaded assembly: </p>
  
  <pre><code>namespace ConsoleApplication1
  {
      class Program
      {
          static Type[] parmTypes;
  
          static void Main(string[] args)
          {
              Assembly asm = Assembly.LoadFile(@"C:\Projects\Dll1.Dll");
              Type typ = asm.GetType("DLL1.Class1", true, true);
  
              var method = typ.GetMethod("add");
              EventInfo eInfo = typ.GetEvents()[0];
              var obj = Activator.CreateInstance(typ);
  
              EventProcessor evProc = new EventProcessor();
              Type myTypeObj = evProc.GetType();
              MethodInfo myMethodInfo = myTypeObj.GetMethod("myEventHandler");
  
              Delegate d = Delegate.CreateDelegate(myTypeObj, myMethodInfo, true); // Error!
              eInfo.AddEventHandler(obj, d);
  
              method.Invoke(obj, new object[] { 1, 0 });
          }
      }
  }
  </code></pre>
  
  <p>But, when run the program I get an error message "Type must derive from Delegate.
  Parameter name: type". What I'm doing wrong here? Or is there any better way to handle this scenario? I also added my event handler class at the end, if it helps. </p>
  
  <pre><code>namespace ConsoleApplication1
  {
      class EventProcessor
      {
          public void myEventHandler(object sender, AddArgs args)
          {
              Console.WriteLine("Event Received.");
          }
      }
  
      public class AddArgs : EventArgs
      {
          private int intResult;
  
          public AddArgs(int _Value)
          {
              intResult = _Value;
          }
  
          public int Result
          {
              get { return intResult; }
          }
      }
  }
  </code></pre>
  
  <p>Thanks in advance.</p>