c# - 获取 Enum<T> 值 描述

标签 c# enums generics bindinglist

我的 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”

我该怎么做?这可能吗?

谢谢。

最佳答案

看看这个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;
   }
} 

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

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

关于c# - 获取 Enum<T> 值 描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/297299/

相关文章:

c# - 如何让 FxCop 与 Sonar 一起使用 C# 解决方案?

c# - 如何使用 SQL 显示字符串枚举值而不是数字值

ruby-on-rails - 使用 Rails I18n 翻译 Ruby 枚举符号?

c# - 我可以将 HashSet<SomeEnumeration> 作为 HashSet<byte> 传递吗?

swift - 我可以使用泛型快速将 NSNumber 转换为 T 吗?

java - 在 Java 泛型声明中使用 'or'

c# - Web 管理面板作为独立程序集

c# - 如何在 C# 中获取当前的短日期时间格式?

javascript - 类比 htmls &lt;script src =""> xaml 标签

c# - VS 2015 方法重载解析行为