C# Metro风格应用程序中的自定义类属性
我试图在Metro风格的应用程序可移植库中的类上定义和检索自定义属性 差不多C# Metro风格应用程序中的自定义类属性,c#,windows-runtime,custom-attributes,system.reflection,portable-class-library,C#,Windows Runtime,Custom Attributes,System.reflection,Portable Class Library,我试图在Metro风格的应用程序可移植库中的类上定义和检索自定义属性 差不多 [AttributeUsage(AttributeTargets.Class)] public class FooAttribute : Attribute { } [Foo] public class Bar { } class Program { static void Main(string[] args) { var attrs = CustomAttributeExten
[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));
}
}
谢谢根据OP:
您需要执行var attrs=CustomAttributeExtensions.GetCustomAttribute(typeof(Bar).GetTypeInfo())
这似乎与相一致,或者也与扩展的含义相一致:
var attr = typeof(Bar).GetTypeInfo().GetCustomAttribute<FooAttribute>();
var attr=typeof(Bar).GetTypeInfo().GetCustomAttribute();
显然,您需要执行var attrs=CustomAttributeExtensions.GetCustomAttribute(typeof(Bar.GetTypeInfo())@user1364325如果上述注释中的代码解决了问题,请将其作为答案发布,并将其标记为已回答。
var attr = typeof(Bar).GetTypeInfo().GetCustomAttribute<FooAttribute>();