title: Dynamic Binding to C# Events Using Reflection
body: <p>I have a WPF control with very poor documentation.</p>
  
  <p>In the codebehind I'd like to reflect over the events that the control fires using <code>GetType().GetEVents()</code> and add a handler to each one which simply prints out the name of the event.</p>
  
  <p>This would allow me to see what interacting with the control is actually doing.</p>
  
  <p>So far I have:</p>
  
  <pre><code>foreach (var e in GetType().GetEvents())
  {
      var name = e.Name;
      var handler = new Action&lt;object,object&gt;( (o1,o2) =&gt;Console.WriteLine(name));
  
      try
      {
          e.AddEventHandler(
                       this,
                       Delegate.CreateDelegate(
                                 e.EventHandlerType,
                                 handler.Target, 
                                 handler.Method
                                 ));
      }
      catch (Exception ex)
      {
          Console.WriteLine( "Failed to bind to event {0}", e.Name);
      }
  }
  </code></pre>
  
  <p>Which seems to work when the event signature is <code>(object,EventArgs)</code> but fails to bind when on certain other events.</p>
  
  <p>Is there a way to do this without necessarily knowing the signature of the event?</p>