微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Metro风格应用程序中的自定义类属性

我试图在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));
    }
}

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

Cannot convert type 'System.Type' to 'System.Reflection.MemberInfo'

谢谢

解决方法

根据OP:

You need to do var attrs = CustomAttributeExtensions.GetCustomAttribute(typeof(Bar).GetTypeIn‌​fo());

这似乎与the documentation一致

原文地址:https://www.jb51.cc/csharp/95212.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐