我正在尝试在一个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版本中是可行的,但针对运行于Metro样式应用程序的便携库时,它会告诉我:
Cannot convert type 'System.Type' to 'System.Reflection.MemberInfo'
感谢
var attrs = CustomAttributeExtensions.GetCustomAttribute(typeof(Bar).GetTypeInfo());
- marc-dworkin