【问题标题】:Get the Enum<T> value Description获取 Enum<T> 值说明
【发布时间】:2010-09-22 18:37:13
【问题描述】:

我的 enumHelper 类包含这些:

public static IList<T> GetValues()
{
  IList<T> list = new List<T>();
  foreach (object value in Enum.GetValues(typeof(T)))
  {
    list.Add((T)value);
  }
  return list;
}

public static string Description(Enum value)
{
  Attribute DescAttribute = LMIGHelper.GetAttribute(value, typeof(DescriptionAttribute));
  if (DescAttribute == null)
    return value.ToString();
  else
    return ((DescriptionAttribute)DescAttribute).Description;
}

我的枚举类似于:

public enum OutputType
{
    File,
    [Description("Data Table")]
    DataTable
}

到目前为止一切顺利。以前的所有工作都很好。 现在我想添加一个新的助手来返回 BindingList>,所以我可以使用

将任何枚举链接到任何组合
BindingList<KeyValuePair<OutputType, string>> list = Enum<OutputType>.GetBindableList();
cbo.datasource=list;
cbo.DisplayMember="Value";
cbo.ValueMember="Key";

为此我补充说:

public static BindingList<KeyValuePair<T, string>> GetBindingList()
{
    BindingList<KeyValuePair<T, string>> list = new BindingList<KeyValuePair<T, string>>();
    foreach (T value in Enum<T>.GetValues())
    {
        string Desc = Enum<T>.Description(value);
        list.Add(new KeyValuePair<T, string>(value, Desc));
    }
    return list;
}

但是“Enum.Description(value)”甚至没有编译: 参数“1”:无法从“T”转换为“System.Enum”

我该怎么做?这可能吗?

谢谢。

【问题讨论】:

标签: c# enums generics bindinglist


【解决方案1】:

看看这个article。您可以使用 System.ComponentModel.DescriptionAttribute 或创建自己的属性来做到这一点:

/// <summary>
/// Provides a description for an enumerated type.
/// </summary>
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field, 
 AllowMultiple = false)]
public sealed class EnumDescriptionAttribute :  Attribute
{
   private string description;

   /// <summary>
   /// Gets the description stored in this attribute.
   /// </summary>
   /// <value>The description stored in the attribute.</value>
   public string Description
   {
      get
      {
         return this.description;
      }
   }

   /// <summary>
   /// Initializes a new instance of the
   /// <see cref="EnumDescriptionAttribute"/> class.
   /// </summary>
   /// <param name="description">The description to store in this attribute.
   /// </param>
   public EnumDescriptionAttribute(string description)
       : base()
   {
       this.description = description;
   }
} 

然后你需要用这个新属性来装饰枚举值:

public enum SimpleEnum
{
   [EnumDescription("Today")]
   Today,

   [EnumDescription("Last 7 days")]
   Last7,

   [EnumDescription("Last 14 days")]
   Last14,

   [EnumDescription("Last 30 days")]
   Last30,

   [EnumDescription("All")]
   All
} 

所有的“魔法”都发生在以下扩展方法中:

/// <summary>
/// Provides a static utility object of methods and properties to interact
/// with enumerated types.
/// </summary>
public static class EnumHelper
{
   /// <summary>
   /// Gets the <see cref="DescriptionAttribute" /> of an <see cref="Enum" /> 
   /// type value.
   /// </summary>
   /// <param name="value">The <see cref="Enum" /> type value.</param>
   /// <returns>A string containing the text of the
   /// <see cref="DescriptionAttribute"/>.</returns>
   public static string GetDescription(this Enum value)
   {
      if (value == null)
      {
         throw new ArgumentNullException("value");
      }

      string description = value.ToString();
      FieldInfo fieldInfo = value.GetType().GetField(description);
      EnumDescriptionAttribute[] attributes =
         (EnumDescriptionAttribute[])
       fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute), false);

      if (attributes != null && attributes.Length > 0)
      {
         description = attributes[0].Description;
      }
      return description;
   }

   /// <summary>
   /// Converts the <see cref="Enum" /> type to an <see cref="IList" /> 
   /// compatible object.
   /// </summary>
   /// <param name="type">The <see cref="Enum"/> type.</param>
   /// <returns>An <see cref="IList"/> containing the enumerated
   /// type value and description.</returns>
   public static IList ToList(this Type type)
   {
      if (type == null)
      {
         throw new ArgumentNullException("type");
      }

      ArrayList list = new ArrayList();
      Array enumValues = Enum.GetValues(type);

      foreach (Enum value in enumValues)
      {
         list.Add(new KeyValuePair<Enum, string>(value, GetDescription(value)));
      }

      return list;
   }
} 

最后,您可以简单地绑定组合框:

combo.DataSource = typeof(SimpleEnum).ToList();

【讨论】:

  • 哦,你可以把它写在你的答案中。
  • 这里使用ArrayList有什么特别的原因吗? List 不是更好吗?
  • @Scott 感谢您的代码。如果我们想返回一个 int 值而不是列表中的枚举。我们可以使用 Convert.toInt(value)。 ref:stackoverflow.com/questions/908543/…希望这对某人有帮助。
【解决方案2】:

你应该改变:

public static string Description(Enum value)
{
  ...
}

public static string Description(T value)
{
   ...
}

所以它接受一个枚举值。现在这里是棘手的地方:你有一个值,但属性装饰保存该值的字段

您实际上需要反映枚举的字段,并根据您获得的值检查每个字段的值(应缓存结果以提高性能):

foreach(var field in typeof(T).GetFields())
{
    T fieldValue;

    try
    {
        fieldValue = (T) field.GetRawConstantValue();
    }
    catch(InvalidOperationException)
    {
        // For some reason, one of the fields returned is {Int32 value__},
        // which throws an InvalidOperationException if you try and retrieve
        // its constant value.
        //
        // I am unsure how to check for this state before
        // attempting GetRawConstantValue().

        continue;
    }

    if(fieldValue == value)
    {
        var attribute = LMIGHelper.GetAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

        return attribute == null ? value.ToString() : attribute.Description;
    }
}

编辑解决后续问题

FillComboFromEnum 方法缺少枚举的类型参数。试试这个:

public static void FillComboFromEnum<T>(ComboBox Cbo, BindingList<KeyValuePair<T, string>> List) where T : struct

请注意,我将类型限制为结构。这不是一个完整的枚举约束,但它比没有更接近。

【讨论】:

    【解决方案3】:

    枚举没有 Description() 方法。您能做的最好的事情就是让您的枚举实现一个具有 Description() 方法的接口。如果你这样做,那么你可以拥有

    public static BindingList<KeyValuePair<T extends _interface_, String>> getBindingList()
    

    然后在里面你可以参考

    T foo = ...?
    foo.Description(...);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 2013-06-06
      • 2016-02-13
      相关资源
      最近更新 更多