title: finding events via reflection c#
body: <p>I have a problem concerning Events and Reflection in C#.
  I'm currently programming with the Microsoft Kinect (SDK 1.7) and want to implement a different click than the "Push-To-Press"-Approach. The ClickEvent itself is declared in the KinectControl class.</p>
  
  <pre><code>    public partial class KinectControl : UserControl
      {
          /**
          \brief Default Click event 
          */
  
          public static readonly RoutedEvent ClickEvent =
              EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(KinectControl)); 
          [...]
  
          public event RoutedEventHandler Click
          {
              add
              {
                  AddHandler(ClickEvent, value);
              }
              remove
              {
                  RemoveHandler(ClickEvent, value);
              }
          }
      } 
  </code></pre>
  
  <p>The Control which should raise this ClickEvent and invoke the proper method is a "Menu_Point"-object which inherits from the KinectControl-class.</p>
  
  <p>After recognizing a click gesture, the "source" respectively "Menu_Point"-object raises the ClickEvent like this:</p>
  
  <pre><code>invokeCtrl.Raise&lt;RoutedEventArgs&gt;("Click", new RoutedEventArgs(KinectControl.ClickEvent));
  </code></pre>
  
  <p>which calls the following method:</p>
  
  <pre><code>        public static void Raise&lt;TEventArgs&gt;(this object source, string eventName, TEventArgs eventArgs) where TEventArgs : EventArgs
      {
          var list = 
              source.GetType().GetFields(BindingFlags.Public | 
              BindingFlags.NonPublic | BindingFlags.Instance | 
              BindingFlags.Static | BindingFlags.SetField | 
              BindingFlags.InvokeMethod | BindingFlags.Instance);
          FieldInfo fieldInfo = 
              source.GetType().GetField(eventName, BindingFlags.Public | 
              BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static |
              BindingFlags.GetField);
  
          var eventDelegate = 
              (MulticastDelegate)fieldInfo.GetValue(source);
  
          if (eventDelegate != null)
          {
              foreach (var handler in eventDelegate.GetInvocationList())
              {
                  handler.Method.Invoke(handler.Target, new object[]{ source, eventArgs });
              }
          }
      }
  </code></pre>
  
  <p>It works fine intil the Raise-method is called and gets to this line:
  <code>FieldInfo fieldInfo = 
                  source.GetType().GetField(eventName, BindingFlags.Public | 
                  BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static |
                  BindingFlags.GetField);</code>
  I don't know why, but the fieldInfo ist always remaining <code>null</code> which causes an exception if <code>eventDelegate</code> is trying to get access to it. Unfortunately I'm not able to find the reason why the ClickEvent is not found in this case. The member <code>eventName</code> has the value "Click" which is the name of the Event in the KinectControl-class. I also searched this site for answers (but none of the solutions helped me so far) and I read all kinds of regarding blogs like DotNetPearls.com but still I can't seem to find the mistake here.
  I hope you can help me out. Thanks in advance!</p>
  
  <p>Edit: added <code>BindingFlags.Static</code>; forgot that in the FieldInfo. Thanks @Michael Gunter. Still doesn't work, though.</p>