代码之家  ›  专栏  ›  技术社区  ›  Corey Downie

如何获得具有反射的静态属性

  •  95
  • Corey Downie  · 技术社区  · 16 年前

    所以这看起来很基本,但我不能让它工作。我有一个对象,我使用反射来获得它的公共属性。其中一个属性是静态的,我不太可能达到它。

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

    对于公共实例属性,上面的代码工作得很好,到目前为止,这是我所需要的全部。假设我可以使用bindingFlags请求其他类型的属性(private、static),但是我似乎找不到正确的组合。

    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 Reflector可以很好地看到静态属性,所以很明显我这里缺少了一些东西。

    8 回复  |  直到 9 年前
        1
  •  110
  •   Ernest    14 年前

    或者看看这个…

    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...
    }
    
        2
  •  40
  •   earlNameless    16 年前

    这是C,但应该给你一个想法:

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

    (您需要或仅非公共和静态)

        3
  •  31
  •   Lee Taylor Dejan.S    9 年前

    有点清楚…

    // 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;
    
        4
  •  27
  •   Corey Downie    16 年前

    好的,所以我的关键是使用.flattenhierarchy绑定标志。我真的不知道为什么我只是凭直觉加上去,然后它就开始起作用了。因此,允许我获取公共实例或静态属性的最终解决方案是:

    obj.GetType.GetProperty(propName, Reflection.BindingFlags.Public _
      Or Reflection.BindingFlags.Static Or Reflection.BindingFlags.Instance Or _
      Reflection.BindingFlags.FlattenHierarchy)
    
        5
  •  6
  •   Jérôme Verstrynge    13 年前
    myType.GetProperties(BindingFlags.Public | BindingFlags.Static |  BindingFlags.FlattenHierarchy);
    

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

        6
  •  2
  •   Andras Zoltan    9 年前

    我只是想为自己澄清这一点,同时使用基于 TypeInfo -何处 BindingFlags 不可靠(取决于目标框架)。

    在“new”反射中,要获取类型(不包括基类)的静态属性,必须执行以下操作:

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

    同时满足只读或只写属性(尽管只写是一个糟糕的主意)。

    这个 DeclaredProperties 成员也不区分具有公共/私有访问器的属性,因此为了过滤可见性,您需要根据需要使用的访问器来进行筛选。例如-假设上述呼叫已返回,您可以执行以下操作:

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

    有一些快捷方法可用-但最终我们都将围绕 类型信息 将来的查询方法/属性。此外,新的API迫使我们从现在开始考虑我们所认为的“私有”或“公共”属性,因为我们必须根据单个访问器过滤自己。

        7
  •  1
  •   Vyas Bharghava    16 年前

    下面的内容似乎对我有用。

    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());
    
    
        }
    }
    
        8
  •  -2
  •   Ken Henderson    16 年前

    试试这个 C# Reflection 链接。

    注意,我认为 BindingFlags.Instance and BindingFlags.Static 是排他性的。