locked
How can I get both description and Value from an enumeration? RRS feed

  • Question

  • Hi
    I have the following enum

     public enum Volume : int
            {
                [Description("Low Volume")]Low,
                [Description("Medium Volume")]Medium,
                [Description("High Volume")]Volume"
              
            }

    I want to populate a Dictionary<string,string>  with respective  value,Description.

    Despite all the fiddling.I could not get it populated.I can get either the values or the names but not both together.


    Can you help?

    Thanks a lot


    Thanks for your help
    Tuesday, September 9, 2008 4:20 PM

Answers

  •         Dictionary<stringstring> enumerationNamesAndValues = new Dictionary<stringstring>();  
     
            foreach (FieldInfo f in t.GetFields(BindingFlags.Public | BindingFlags.Static))  
                enumerationNamesAndValues.Add(f.GetCustomAttributes(typeof(DescriptionAttribute), false)[0].GetType().GetProperty("Description").GetValue(f.GetCustomAttributes(typeof(DescriptionAttribute), false)[0], null).ToString(), f.Name);  
     
            foreach (KeyValuePair<stringstring> kvp in enumerationNamesAndValues)  
                Console.WriteLine("{0} - {1}", kvp.Key, kvp.Value); 

    David Morton - http://blog.davemorton.net/
    • Proposed as answer by Roman Rozinov Tuesday, September 9, 2008 8:39 PM
    • Marked as answer by nobugz Wednesday, September 10, 2008 5:12 AM
    Tuesday, September 9, 2008 4:41 PM

All replies

  •         Dictionary<stringstring> enumerationNamesAndValues = new Dictionary<stringstring>();  
     
            foreach (FieldInfo f in t.GetFields(BindingFlags.Public | BindingFlags.Static))  
                enumerationNamesAndValues.Add(f.GetCustomAttributes(typeof(DescriptionAttribute), false)[0].GetType().GetProperty("Description").GetValue(f.GetCustomAttributes(typeof(DescriptionAttribute), false)[0], null).ToString(), f.Name);  
     
            foreach (KeyValuePair<stringstring> kvp in enumerationNamesAndValues)  
                Console.WriteLine("{0} - {1}", kvp.Key, kvp.Value); 

    David Morton - http://blog.davemorton.net/
    • Proposed as answer by Roman Rozinov Tuesday, September 9, 2008 8:39 PM
    • Marked as answer by nobugz Wednesday, September 10, 2008 5:12 AM
    Tuesday, September 9, 2008 4:41 PM
  • Perhaps some explanation.  Enumeration values are stored as public static fields.  So you have to get the public static fields via reflection of the type of enumeration.  After that, you'd need to get the Custom attribute "Description", which is type DescriptionAttribute, from the field.  In DescriptionAttribute, there's a "Description" property, which is what you pass in when declaring the attribute.  This is the value you want to get to get your more complicated description.

    William Wegerson, AKA OmegaMan has a blog on this at the following link:

    http://www.omegacoder.com/?p=28
    David Morton - http://blog.davemorton.net/
    Tuesday, September 9, 2008 4:45 PM
  • Fantastic!!

    That worked a treat!!

    thanks.again
    Thanks for your help
    Tuesday, September 9, 2008 7:02 PM