In C#, how can I tell if a property is static? (.Net CF 2.0)

情到浓时终转凉″ 提交于 2019-12-03 22:09:42

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

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

ctacke

Why not use

type.GetProperties(BindingFlags.Static)

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()

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

propertyInfo.GetAccessors(true)[0].IsStatic;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!