【问题标题】:GetCustomAttribute returns nullGetCustomAttribute 返回 null
【发布时间】:2012-12-26 11:14:08
【问题描述】:

谁能向我解释为什么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;
    }
}

【问题讨论】:

  • 也许我应该改写我的问题。我明白为什么会抛出NullReferenceException。我不明白为什么Value.GetType().GetCustomAttribute 返回null。

标签: c# enums custom-attributes


【解决方案1】:

因为您尝试检索的属性尚未应用于该类型;已经应用到现场了。

因此,您需要在 FieldInfo 对象上调用它,而不是在类型对象上调用 GetCustomAttributes。换句话说,你需要做更多这样的事情:

typeof(Foo).GetField(value.ToString()).GetCustomAttributes...

【讨论】:

  • 如何将其应用于字段而不是类型?
  • 您已将属性应用到字段
  • @TylerCrompton 正如 Dhawalk 所说,在您的示例代码中,属性 is 应用于该字段。这就是你遇到这个问题的原因。这也是属性自己的 AttributeUsage 属性用于 AttributeTargets.Field 的原因。我已经改写并略微扩展了答案。
【解决方案2】:

phoog 对问题的解释是正确的。如果您想了解如何检索枚举值的属性的示例,请查看this answer。

【讨论】:

  • +2 OP 在理解所提供链接中的代码时绝对不会有任何问题
【解决方案3】:

您的属性处于字段级别,而 Value.GetType().GetCustomAttribute&lt;BarAttribute&gt;(true).Name 将返回应用于枚举 Foo 的属性

【讨论】:

    【解决方案4】:

    我最终改写成这样:

    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;
        }
    }
    

    【讨论】:

      【解决方案5】:

      我认为你必须像这样重写 FooExtension:

      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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-25
        • 1970-01-01
        • 2013-06-17
        • 2020-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-21
        • 2020-06-19
        相关资源
        最近更新 更多