Metro风格应用中的自定义类属性

mar*_*kin 10 c# custom-attributes system.reflection portable-class-library windows-runtime

我正在尝试在Metro Style App便携式库中的类上定义和检索自定义属性.

就像是

[AttributeUsage(AttributeTargets.Class)]
public class FooAttribute : Attribute
{
}

[Foo]
public class Bar
{
}


class Program
{
    static void Main(string[] args)
    {
        var attrs = CustomAttributeExtensions.GetCustomAttribute<FooAttribute>(typeof(Bar));
    }
}
Run Code Online (Sandbox Code Playgroud)

这适用于普通的4.5,但在一个便携式图书馆中,它针对地铁风格的应用程序,它告诉我

Cannot convert type 'System.Type' to 'System.Reflection.MemberInfo'
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 5

或者,过度利用扩展,因为它们意味着:

var attr = typeof(Bar).GetTypeInfo().GetCustomAttribute<FooAttribute>();
Run Code Online (Sandbox Code Playgroud)