How to tell if PropertyInfo defines a static or instance property

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Given a PropertyInfo object, how can I determine if the property that it
describes is a static or instance property? The same question applies to
EventInfo.

MethodInfo, ConstructorInfo and FieldInfo all have an IsStatic member, but
PropertyInfo and EventInfo do not have this member, which seems a bit strange.
 
Robert,
MethodInfo, ConstructorInfo and FieldInfo all have an IsStatic member, but
PropertyInfo and EventInfo do not have this member, which seems a bit strange.

Just check the static-ness of one of the accessor methods.

bool isPropStatic =
(propInfo.CanRead && propInfo.GetGetMethod().IsStatic) ||
(propInfo.CanWrite && propInfo.GetSetMethod().IsStatic);



Mattias
 
Since properties internally consists of set and get methods, there is a
method in PropertyInfo.GetGetMethod and PropertyInfo.GetSetMethod which
retrieves you the method where you can check wheather they are static or
not. You only need to check one of them because the values must be the same
for IsStatic.

The same is true for EventInfo: use GetAddMethod , GetRaiseMethod or
GetRemoveMethod.
 
Back
Top