【问题标题】:Foreach enum values without castingForeach 枚举值而不进行强制转换
【发布时间】:2016-03-02 13:48:37
【问题描述】:

我必须强制转换枚举,但我希望它尽可能通用。如何将Cast<XX>() 部分替换为propertyType

foreach (var prop in MainType.GetProperties().Where(x => x.PropertyType.IsEnum))
{
     var x = new { name = prop.Name, values = new List<object>() };

     foreach (var v in Enum.GetValues(prop.PropertyType).Cast<XX>())
         x.values.Add(new { p = v.GetAttribute<DescriptionAttribute>().Description, 
                            c = v.GetAttribute<DefaultValueAttribute>().Value });

     o.Add(x);
}


    public static TAttribute GetAttribute<TAttribute>(this Enum value) where TAttribute : Attribute
    {
        var type = value.GetType();
        var name = Enum.GetName(type, value);
        return type.GetField(name)
            .GetCustomAttributes(false)
            .OfType<TAttribute>()
            .SingleOrDefault();
    }

public enum JobApplicationState : short
{
    [Description("Active")]
    [DefaultValue(typeof(string), "bg-primary text-highlight")]
    Active = 1,
    [Description("Passive")]
    [DefaultValue(typeof(string), "bg-grey text-highlight")]
    Passive = 2,
    [Description("Rejected")]
    [DefaultValue(typeof(string), "bg-danger text-highlight")]
    Rejected = 3,
    [Description("Accepted")]
    [DefaultValue(typeof(string), "bg-success text-highlight")]
    Accepted = 4
}

成功了!

foreach (MemberInfo m in prop.PropertyType.GetFields())
{
    if (m.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault() != null && m.GetCustomAttributes(typeof(DefaultValueAttribute), true).FirstOrDefault() != null)
        {
             x.values.Add(
                 new
                 {
                     p = ((DescriptionAttribute)m.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault()).Description,
                     c = ((DefaultValueAttribute)m.GetCustomAttributes(typeof(DefaultValueAttribute), true).FirstOrDefault()).Value
        });
    } 
}

【问题讨论】:

  • 我不明白。你不需要投吧?我的意思是,它是一个成员,可以有属性。
  • 为什么不使用底层数字原始类型? (例如,转换为 int)
  • 我不需要枚举类型来使用 GetAttribute 方法吗?
  • @Mert Nope。在 MSDN 上查找 MemberInfo.GetAttribute。字段是成员。
  • @PTwr 我知道我不是唯一一个感到困惑的人。整数没有属性。我认为他打算做的是从字段(例如FieldInfo)中获取属性。

标签: c# .net reflection enums


【解决方案1】:

你应该意识到枚举只不过是一个整数值的命名包装器。我在这里描述了一些关于枚举如何工作的细节:Cast int to enum in C#

这里的问题是从枚举中获取属性值。正如您可能想象的那样,这个问题是关于从 type 获取属性,而不是关于从 value 获取属性(没有这样的事情)。尽管如此,如果您调用像 void Foo&lt;T&gt;(T myEnum) 这样的方法,类型 T 将保存所有必要的信息,即使“在现实生活中” myEnum 的值是作为整数类型传递的。

由此您还可以推断出您实际上是在寻找TMemberInfo 的属性,因为您正在寻找该类型的成员(例如枚举)。同样,所有细节都在我关于枚举如何工作的帖子中。因此答案是:

foreach (MemberInfo m in prop.PropertyType.GetFields())
{
    // use m.GetAttribute(...) 
}

【讨论】:

    【解决方案2】:

    你为什么不直接投到Enum

    foreach (var v in Enum.GetValues(prop.PropertyType).Cast<Enum>())
    

    现在您可以在返回列表中的每个元素上调用您的扩展方法。

    v.GetAttribute<MyType>()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多