Cra*_*des 28 c# reflection compact-framework
FieldInfo有一个IsStatic成员,但PropertyInfo没有.我想我只是忽略了我需要的东西.
Type type = someObject.GetType();
foreach (PropertyInfo pi in type.GetProperties())
{
// umm... Not sure how to tell if this property is static
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*nke 42
要确定属性是否为静态,必须通过调用GetGetMethod或GetSetMethod方法获取get或set访问器的MethodInfo,并检查其IsStatic属性.
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx
为什么不用
type.GetProperties(BindingFlags.Static)
Run Code Online (Sandbox Code Playgroud)
作为针对所提出问题的快速简便的实际解决方案,您可以使用以下方法:
propertyInfo.GetAccessors(true)[0].IsStatic;
Run Code Online (Sandbox Code Playgroud)
更好的解决方案
public static class PropertyInfoExtensions
{
public static bool IsStatic(this PropertyInfo source, bool nonPublic = false)
=> source.GetAccessors(nonPublic).Any(x => x.IsStatic);
}
Run Code Online (Sandbox Code Playgroud)
用法:
property.IsStatic()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9704 次 |
最近记录: |