Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jeffwilcox
Created April 20, 2012 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffwilcox/2431936 to your computer and use it in GitHub Desktop.
Save jeffwilcox/2431936 to your computer and use it in GitHub Desktop.
Is a PropertyInfo a static property?
public static bool IsStatic(this PropertyInfo propertyInfo)
{
return ((propertyInfo.CanRead && propertyInfo.GetMethod.IsStatic) ||
(propertyInfo.CanWrite && propertyInfo.SetMethod.IsStatic));
}
// So a replacement for getting private and public non-static properties in a type...
public static bool IsStatic(this PropertyInfo propertyInfo)
{
return ((propertyInfo.CanRead && propertyInfo.GetMethod.IsStatic) ||
(propertyInfo.CanWrite && propertyInfo.SetMethod.IsStatic));
}
public static PropertyInfo[] GetPublicAndNonPublicInstanceProperties(this Type type)
{
var list = new List<PropertyInfo>();
var props = type.GetTypeInfo().DeclaredProperties;
foreach (var prop in props)
{
if (!prop.IsStatic())
{
list.Add(prop);
}
}
return list.ToArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment