我试图在Metro Style App便携式库中定义和检索一个类的自定义属性.
就像是@H_502_3@
[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工作,但在一个便携式图书馆,针对地铁风格的应用程序,它告诉我@H_502_3@
Cannot convert type 'System.Type' to 'System.Reflection.MemberInfo'
谢谢@H_502_3@
解决方法
根据OP:
You need to do var attrs = CustomAttributeExtensions.GetCustomAttribute(typeof(Bar).GetTypeInfo());@H_502_3@
这似乎与the documentation一致@H_502_3@