Get the Enum value Description

后端 未结 3 1963
离开以前
离开以前 2020-12-18 07:10

I have my enumHelper class that contains these:

public static IList GetValues()
{
  IList list = new List();
  foreach (object val         


        
相关标签:
3条回答
  • 2020-12-18 08:01

    You should change:

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

    to

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

    so it accepts a value of the enumeration. Now here is where it gets tricky: you have a value, but attributes decorate the field which holds the value.

    You actually need to reflect over the enumeration's fields and check the value of each against the value you've been given (results should be cached for performance):

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

    Edit addressing the follow-up question

    The FillComboFromEnum method is missing the type parameter for the enum. Try this:

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

    Notice I constrained the type to be a struct. It's not a full enumeration constraint, but it's closer than nothing.

    0 讨论(0)
  • 2020-12-18 08:02

    Take a look at this article. You can do this using the System.ComponentModel.DescriptionAttribute or creating your own attribute:

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

    You then need to decorate the enum values with this new attribute:

    public enum SimpleEnum
    {
       [EnumDescription("Today")]
       Today,
    
       [EnumDescription("Last 7 days")]
       Last7,
    
       [EnumDescription("Last 14 days")]
       Last14,
    
       [EnumDescription("Last 30 days")]
       Last30,
    
       [EnumDescription("All")]
       All
    } 
    

    All of the "magic" takes place in the following extension methods:

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

    Finally, you can then simply bind the combobox:

    combo.DataSource = typeof(SimpleEnum).ToList();
    
    0 讨论(0)
  • 2020-12-18 08:04

    Enum doesn't have a Description() method. The best you could do is have your enum implement an interface that has the Description() method. If you do that, then you can have

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

    and then inside of that you can refer to

    T foo = ...?
    foo.Description(...);
    
    0 讨论(0)
提交回复
热议问题