2012-10-19 29 views
0

我怎樣才能得到所有的靜態屬性的PropertyInfo的,與FlattenHierachy在新換的應用程序.NET個人資料?獲取靜態的PropertyInfo淨「芯」 4.5

我想這個代碼,但App-.Net:

const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy; 
return type.GetProperties(bindingFlags); 
+0

我已經做了一些嘗試... – Henrik

回答

3

所有這些測試都通過關於Win8的RTM/.NET核心4.5。

namespace Automatonymous.Tests 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using NUnit.Framework; 
    using System.Reflection; 

    class SuperTarget 
    { 
     public static string StaticProp { get; set; } 
     public string InstanceProp { get; set; } 
    } 

    class SubTarget 
     : SuperTarget 
    { 
     public string AnotherProp { get; private set; } 
    } 

    class PrivateStatics 
     : SuperTarget 
    { 
     static string CanWeGetPrivates { get; set; } 
    } 

    class StaticsNoGetter 
     : SuperTarget 
    { 
     public static string ZupMan { set; private get; } 
    } 

    [TestFixture] 
    public class when_getting_static_properties 
    { 
     [Test] 
     public void can_get_property_on_stand_alone_class() 
     { 
      var props = GetAllStaticProperties(typeof(SuperTarget)); 
      Assert.That(props.Count(), Is.EqualTo(1)); 
      Assert.That(props.First().Name, Is.EqualTo("StaticProp")); 
     } 

     [Test] 
     public void can_get_single_property_on_super_from_sub() 
     { 
      var props = GetAllStaticProperties(typeof(SubTarget)); 
      Assert.That(props.Count(), Is.EqualTo(1)); 
      Assert.That(props.First().Name, Is.EqualTo("StaticProp")); 
     } 

     [Test] 
     public void can_get_private_static_properties() 
     { 
      var props = GetAllStaticProperties(typeof(PrivateStatics)); 
      Assert.That(props.Count(), Is.EqualTo(2)); 
      var names = props.Select(x => x.Name); 
      CollectionAssert.Contains(names, "CanWeGetPrivates"); 
      CollectionAssert.Contains(names, "StaticProp"); 
     } 

     [Test] 
     public void can_get_even_with_private_getter() 
     { 
      var props = GetAllStaticProperties(typeof(StaticsNoGetter)); 
      Assert.That(props.Count(), Is.EqualTo(2)); 
      var names = props.Select(x => x.Name); 
      CollectionAssert.Contains(names, "ZupMan"); 
      CollectionAssert.Contains(names, "StaticProp"); 
     } 

     [Test] 
     public void can_get_with_no_hierarchy() 
     { 
      var props = GetAllStaticProperties(typeof(StaticsNoGetter), false); 
      Assert.That(props.Count(), Is.EqualTo(1)); 
      Assert.That(props.First().Name, Is.EqualTo("ZupMan")); 
     } 

     static IEnumerable<PropertyInfo> GetAllStaticProperties(Type type, 
      bool flattenHierachy = true) 
     { 
      var info = type.GetTypeInfo(); 

      if (info.BaseType != null && flattenHierachy) 
       foreach (var prop in GetAllStaticProperties(info.BaseType, true)) 
        yield return prop; 

      var props = info.DeclaredMethods 
          .Where(x => x.IsSpecialName && x.Name.StartsWith("get_") && x.IsStatic) 
          .Select(x => info.GetDeclaredProperty(x.Name.Substring("get_".Length))); 

      foreach (var propertyInfo in props) 
       yield return propertyInfo; 
     } 
    } 
}