0

如何在新的 for-apps .Net 配置文件中使用 FlattenHierachy 获取所有静态属性的 PropertyInfo?

我想要这段代码,但对于 App-.Net:

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

1 回答 1

3

所有这些测试都在 Win8 RTM/.Net Core 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;
        }
    }
}
于 2012-10-19T19:16:07.143 回答