[ACCEPTED]-In C#, how can I tell if a property is static? (.Net CF 2.0)-compact-framework

Accepted answer
Score: 55

To determine whether a property is static, you 4 must obtain the MethodInfo for the get or 3 set accessor, by calling the GetGetMethod 2 or the GetSetMethod method, and examine 1 its IsStatic property.

http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx

Score: 11

As an actual quick and simple solution to 1 the question asked, you can use this:

propertyInfo.GetAccessors(true)[0].IsStatic;
Score: 9

Better solution

public static class PropertyInfoExtensions
{
    public static bool IsStatic(this PropertyInfo source, bool nonPublic = false) 
        => source.GetAccessors(nonPublic).Any(x => x.IsStatic);
}

Usage:

property.IsStatic()

0

Score: 8

Why not use

type.GetProperties(BindingFlags.Static)

0

More Related questions