Search code examples
c#.netreflectionstaticmemberinfo

Determine if event is static using reflection


I have a System.Reflection.EventInfo object, and I want to know whether the event described by this object is static or not. Unlike System.Reflection.MethodInfo, EventInfo does not have IsStatic property that would tell me what I need. So, how can I do it in C#?

Also, if I have a MemberInfo object that describes some member of my class (could be property, method, field, etc.), how can I determine if this member is static or not? Is the only way to do it to cast my MemberInfo object into the needed type (into MethodInfo if this was a method, for example) and then check if this member is static?


Solution

  • An Event, when declared, under the hood turns into a special method typed as a delegate with an Add and Remove methods.

    What you can do, is check the Add method being generated to see if it is static:

    var isStaticEvent = eventInfo.GetAddMethod().IsStatic;