Compartilhar via


Using reflection to determine if an event is static

So if you happen to have an EventInfo class and need to determine if the event is static or instance, it isn't actually immeidately clear if this is supported. Most of the MemberInfo basic types expose a property named IsStatic that indicates if this member is static or not. If you've ever worked with the MethodInfo class to dynamically invoke a method you're probably familiy with this flag. However when you go to check out the EventInfo class there's no comperable property. Well it turns out there is on the RuntimeEventInfo class but it happens to be protected. You could always use a bit of reflection and crack that item open and look at the flag but there's actually a simpler solution. An event really is related to two methods that just happen to expose particular CLR OpCode that indicate these are not "regular" methods (similar in fact to how the CLR figures out if a method is actually not a "regular" method but instead an operator overload) and instead are used in event delegate chaining. So in our case when we write in C# the following event:

public EventHandler MyEvent;

What we are really doing is actually using shorthand to write this:

pubic EventHandler MyEvent
{
  Add
  {
    //Wire event code to a field
  }
  Remove
  {
    //Unwire event code from a field
  }
}

There's no magic here at work. If you want to know the specifics I'd suggest you check out some of Jeffrey Richter's books such as CLR via C#.

So to recap:

  1. An event is really a type of special method typed as a particular delegate
  2. They have an Add / Remove operation
  3. They are backed by a field
  4. You can reflect on events with the  EventInfo type
  5. You can reflect on methods with the MethodInfo type
  6. The MethodInfo type has an IsStatic property

So now all that is left is putting the various parts of the puzzle together. Take a second to here and see if you can figure this out and then read on...

That means in order to determine if an event is static or not all you need to do is get a reference to one of the two MethodInfo reflection object representing either the Add or Remove operation. So where can you get that? Well that's found right on the EventInfo!

var isStaticEvent = anEventInfo.GetAddMethod().IsStatic;

Honestly that's it folks!

You might ask as to why the CLR team didn't just supply a convienence property to the EventInfo class so that you didn't have to hunt this down. Well there's a good reason. Remember item 2 above? There's TWO methods here at work. That means, weird as it might seem, that one can be static and the other can be instance. I cannot think of a CLR language that exposes that ability but it's there in the raw power of IL (like plenty of other goodness that no language, or only some, show off ... VB filtered exceptions come to mind).