[ACCEPTED]-In C#, how can I tell if a property is static? (.Net CF 2.0)-compact-framework
Accepted answer
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
As an actual quick and simple solution to 1 the question asked, you can use this:
propertyInfo.GetAccessors(true)[0].IsStatic;
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
Why not use
type.GetProperties(BindingFlags.Static)
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.