如何获得与reflection静态属性

所以这似乎很基本,但我不能得到它的工作。 我有一个对象,我正在使用reflection来获取它的公共属性。 其中一个属性是静态的,我没有运气得到它。

Public Function GetProp(ByRef obj As Object, ByVal propName as String) as PropertyInfo Return obj.GetType.GetProperty(propName) End Function 

上面的代码工作正常公共实例属性,到目前为止,我所需要的。 据说我可以使用BindingFlags来请求其他types的属性(私人,静态),但我似乎无法find正确的组合。

 Public Function GetProp(ByRef obj As Object, ByVal propName as String) as PropertyInfo Return obj.GetType.GetProperty(propName, Reflection.BindingFlags.Static Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public) End Function 

但是,请求任何静态成员仍然没有返回。 .NET的reflection器可以看到静态属性就好了,所以很明显,我在这里失去了一些东西。

或者只是看看这个…

 Type type = typeof(MyClass); // MyClass is static class with static properties foreach (var p in type.GetProperties()) { var v = p.GetValue(null, null); // static classes cannot be instanced, so use null... } 

这是C#,但应该给你的想法:

 public static void Main() { typeof(Program).GetProperty("GetMe", BindingFlags.NonPublic | BindingFlags.Static); } private static int GetMe { get { return 0; } } 

(你只需要OR NonPublic和Static)

好,所以我的关键是使用.FlattenHierarchy BindingFlag。 我真的不知道为什么我只是把它加在一个预感,并开始工作。 所以最终的解决scheme,让我获得公共实例或静态属性是:

 obj.GetType.GetProperty(propName, Reflection.BindingFlags.Public _ Or Reflection.BindingFlags.Static Or Reflection.BindingFlags.Instance Or _ Reflection.BindingFlags.FlattenHierarchy) 

有点清晰…

 // Get a PropertyInfo of specific property type(T).GetProperty(....) PropertyInfo propertyInfo; propertyInfo = typeof(TypeWithTheStaticProperty) .GetProperty("NameOfStaticProperty", BindingFlags.Public | BindingFlags.Static); // Use the PropertyInfo to retrieve the value from the type by not passing in an instance object value = propertyInfo.GetValue(null, null); // Cast the value to the desired type ExpectedType typedValue = (ExpectedType) value; 
 myType.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); 

这将返回静态基类或特定types的所有静态属性,也可能返回子对象。

下面似乎为我工作。

 using System; using System.Reflection; public class ReflectStatic { private static int SomeNumber {get; set;} public static object SomeReference {get; set;} static ReflectStatic() { SomeReference = new object(); Console.WriteLine(SomeReference.GetHashCode()); } } public class Program { public static void Main() { var rs = new ReflectStatic(); var pi = rs.GetType().GetProperty("SomeReference", BindingFlags.Static | BindingFlags.Public); if(pi == null) { Console.WriteLine("Null!"); Environment.Exit(0);} Console.WriteLine(pi.GetValue(rs, null).GetHashCode()); } } 

只是想澄清这一点为我自己,而使用基于TypeInfo的新的reflectionAPI – 其中BindingFlags不可靠可用(取决于目标框架)。

在'new'reflection中,为了获得types(不包括基类)的静态属性,你必须做如下的事情:

 IEnumerable<PropertyInfo> props = type.GetTypeInfo().DeclaredProperties.Where(p => (p.GetMethod != null && p.GetMethod.IsStatic) || (p.SetMethod != null && p.SetMethod.IsStatic)); 

迎合只读或只写属性(尽pipe只写是一个可怕的想法)。

DeclaredProperties成员也不区分具有公共/私有访问者的属性 – 所以要过滤可见性,则需要根据您需要使用的访问者来完成。 例如 – 假设上述呼叫已经返回,您可以执行以下操作:

 var publicStaticReadable = props.Where(p => p.GetMethod != null && p.GetMethod.IsPublic); 

有一些快捷方法可用 – 但最终我们都将在未来围绕TypeInfo查询方法/属性编写更多的扩展方法。 此外,新的API迫使我们从现在开始思考我们认为的“私人”或“公共”财产,因为我们必须根据个人访问者来筛选自己。

试试这个C#reflection链接。

注意我认为BindingFlags.Instance和BindingFlags.Static是独占的。