0

I want to populate a list that provides both enum values and descriptions. How do I do this in C#?

I know how to create a list of values but I also want to include the descriptions. Here's what my enums looks like:

using System.ComponentModel;

public enum BusinessCategory
{
   [Description("Computers & Internet")]
   ComputersInternet = 1,
   [Description("Finance & Banking")]
   FinanceBanking = 2,
   [Description("Healthcare")]
   Healthcare = 3,
   [Description("Manufacturing")]
   Manufacturing = 4
}

I'd like my list to look like:

[
   { 1, "Computers & Internet" },
   { 2, "Finance & Banking" },
   { 3, "Healthcare" },
   { 4, "Manufacturing" }
]
Sam
  • 26,817
  • 58
  • 206
  • 383
  • 2
    Possible duplicate of [Getting attributes of Enum's value](http://stackoverflow.com/questions/1799370/getting-attributes-of-enums-value) – Dmitry Jan 26 '16 at 20:26

3 Answers3

5

You'll get your list to loop through by calling Enum.GetValues(typeof(BusinessCategory)).

After that, you can retrieve the description for each value like discussed here:

Community
  • 1
  • 1
Waescher
  • 5,361
  • 3
  • 34
  • 51
1

At first, you need to get all possible values of BusinessCategory via calling Enum.GetValues(typeof(BusinessCategory)).Cast<BusinessCategory>();

After you need to get description attribute values, you should use reflection. Here is the code (you can run it https://dotnetfiddle.net/cHgeAN):

using System;
using System.ComponentModel;
using System.Reflection;
using System.Linq;

public enum BusinessCategory
{
   [Description("Computers & Internet")]
   ComputersInternet = 1,
   [Description("Finance & Banking")]
   FinanceBanking = 2,
   [Description("Healthcare")]
   Healthcare = 3,
   [Description("Manufacturing")]
   Manufacturing = 4
}

public class Program
{
    public static void Main()
    {
        // Get all possible values
        var values = Enum.GetValues(typeof(BusinessCategory)).Cast<BusinessCategory>();
        foreach(var v in values)
        {
            // Write to the console info about each value
            Console.WriteLine("{0}[{1}] => {2}", v, (int)v, v.GetEnumDescription());
        }
    }
}

public static class EnumExtensions
{
    public static string GetEnumDescription(this Enum value, string defaultValue = null)
    {
        return value.GetEnumAttribute<DescriptionAttribute>(a => a.Description, defaultValue);
    }

    private static string GetEnumAttribute<TAttr>(this Enum value, Func<TAttr, string> expr, string defaultValue = null)where TAttr : Attribute
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());
        var attributes = fi.GetCustomAttributes<TAttr>(false).ToArray();
        return (attributes != null && attributes.Length > 0) ? expr(attributes.First()) : (defaultValue ?? value.ToString());
    }
}
Artavazd Balayan
  • 2,353
  • 1
  • 16
  • 25
0

You could do something with Reflection and LINQ along the lines of:

var enums = typeof(BusinessCategory).GetFields().Skip(1)
    .Select(x => Tuple.Create((BusinessCategory)x.GetValue(null), 
    x.GetCustomAttributes<DescriptionAttribute>().First().Description));

This will return a collection of tuples mapping the Enums to their description attributes.

I'm having to skip the first element as I can't find a BindingFlagsconfiguration that will not return a hidden int field value__ from the enum - any ideas there will be great!

TVOHM
  • 2,740
  • 1
  • 19
  • 29