C# GetCustomAttribute返回null
有人能给我解释一下为什么C# GetCustomAttribute返回null,c#,enums,custom-attributes,C#,Enums,Custom Attributes,有人能给我解释一下为什么Value.GetType().GetCustomAttribute返回null?我已经看了十个不同的教程,介绍了如何获取枚举类型成员的属性。无论我使用哪个GetCustomAttribute*方法,都不会返回任何自定义属性 using System; using System.ComponentModel; using System.Reflection; public enum Foo { [Bar(Name = "Bar")] Baz, } [A
Value.GetType().GetCustomAttribute
返回null
?我已经看了十个不同的教程,介绍了如何获取枚举类型成员的属性。无论我使用哪个GetCustomAttribute*
方法,都不会返回任何自定义属性
using System;
using System.ComponentModel;
using System.Reflection;
public enum Foo
{
[Bar(Name = "Bar")]
Baz,
}
[AttributeUsage(AttributeTargets.Field)]
public class BarAttribute : Attribute
{
public string Name;
}
public static class FooExtensions
{
public static string Name(this Foo Value)
{
return Value.GetType().GetCustomAttribute<BarAttribute>(true).Name;
}
}
使用系统;
使用系统组件模型;
运用系统反思;
公众谘询委员会
{
[Bar(Name=“Bar”)]
巴兹,
}
[属性设置(AttributeTargets.Field)]
公共类BarAttribute:属性
{
公共字符串名称;
}
公共静态类扩展
{
公共静态字符串名称(此Foo值)
{
返回值.GetType().GetCustomAttribute(true).Name;
}
}
因为您试图检索的属性尚未应用于该类型;它已应用于该领域
因此,您需要在FieldInfo对象上调用它,而不是在type对象上调用GetCustomAttributes。换句话说,您需要做更多类似的事情:
typeof(Foo).GetField(value.ToString()).GetCustomAttributes...
public static class FooExtensions
{
public static string Name(this Foo Value)
{
string rv = string.Empty;
FieldInfo fieldInfo = Value.GetType().GetField(Value.ToString());
if (fieldInfo != null)
{
object[] customAttributes = fieldInfo.GetCustomAttributes(typeof (BarAttribute), true);
if(customAttributes.Length>0 && customAttributes[0]!=null)
{
BarAttribute barAttribute = customAttributes[0] as BarAttribute;
if (barAttribute != null)
{
rv = barAttribute.Name;
}
}
}
return rv;
}
}
public static class FooExtensions
{
public static string Name(this Foo Value)
{
var Type = Value.GetType();
var Name = Enum.GetName(Type, Value);
if (Name == null)
return null;
var Field = Type.GetField(Name);
if (Field == null)
return null;
var Attr = Field.GetCustomAttribute<BarAttribute>();
if (Attr == null)
return null;
return Attr.Name;
}
}
您的属性位于字段级别,而
Value.GetType().GetCustomAttribute(true).Name
将返回应用于enum Foo的属性phoog对问题的解释是正确的。如果您想了解如何检索枚举值上的属性的示例,请查看。我认为您必须像这样重写FooExtension:
typeof(Foo).GetField(value.ToString()).GetCustomAttributes...
public static class FooExtensions
{
public static string Name(this Foo Value)
{
string rv = string.Empty;
FieldInfo fieldInfo = Value.GetType().GetField(Value.ToString());
if (fieldInfo != null)
{
object[] customAttributes = fieldInfo.GetCustomAttributes(typeof (BarAttribute), true);
if(customAttributes.Length>0 && customAttributes[0]!=null)
{
BarAttribute barAttribute = customAttributes[0] as BarAttribute;
if (barAttribute != null)
{
rv = barAttribute.Name;
}
}
}
return rv;
}
}
public static class FooExtensions
{
public static string Name(this Foo Value)
{
var Type = Value.GetType();
var Name = Enum.GetName(Type, Value);
if (Name == null)
return null;
var Field = Type.GetField(Name);
if (Field == null)
return null;
var Attr = Field.GetCustomAttribute<BarAttribute>();
if (Attr == null)
return null;
return Attr.Name;
}
}
最后我把它改写成这样:
typeof(Foo).GetField(value.ToString()).GetCustomAttributes...
public static class FooExtensions
{
public static string Name(this Foo Value)
{
string rv = string.Empty;
FieldInfo fieldInfo = Value.GetType().GetField(Value.ToString());
if (fieldInfo != null)
{
object[] customAttributes = fieldInfo.GetCustomAttributes(typeof (BarAttribute), true);
if(customAttributes.Length>0 && customAttributes[0]!=null)
{
BarAttribute barAttribute = customAttributes[0] as BarAttribute;
if (barAttribute != null)
{
rv = barAttribute.Name;
}
}
}
return rv;
}
}
public static class FooExtensions
{
public static string Name(this Foo Value)
{
var Type = Value.GetType();
var Name = Enum.GetName(Type, Value);
if (Name == null)
return null;
var Field = Type.GetField(Name);
if (Field == null)
return null;
var Attr = Field.GetCustomAttribute<BarAttribute>();
if (Attr == null)
return null;
return Attr.Name;
}
}
公共静态类扩展
{
公共静态字符串名称(此Foo值)
{
var Type=Value.GetType();
var Name=Enum.GetName(类型、值);
if(Name==null)
返回null;
var Field=Type.GetField(名称);
如果(字段==null)
返回null;
var Attr=Field.GetCustomAttribute();
if(Attr==null)
返回null;
返回属性名;
}
}
如何将其应用于字段而不是类型?您已将该属性应用于feild@TylerCrompton正如Dhawalk所说,在示例代码中,属性应用于字段。这就是你有这个问题的原因。这也是属性自身的AttributeUsage属性用于AttributeTargets.Field的原因。我已经改写并稍微扩展了答案。也许我应该改写我的问题。我理解为什么会抛出NullReferenceException
。我不明白的是为什么Value.GetType().GetCustomAttribute
返回null
+2 OP在理解提供的链接中的代码时肯定不会有任何问题