[RESOLVED]obtaining the descriptors of an enum when populating a list

HI All,

I have an ENUM declared in the form

public enum PayPeriodTypeList
    {
        Undefined = 0,
        [Description("Monthly")]
        Monthly = 1,
        [Description("Weekly")]
        Weekly = 3,
        [Description("Two Weekly")]
        Two_weekly = 4,
        [Description("Four Weekly")]
        Four_weekly = 5,

if i do:

model.PayPeriods = Enum.GetValues(typeof(PayPeriodTypeList)).Cast<PayPeriodTypeList>().ToList();

when I look in the list, it has entries such as ‘Two_weekly’ as opposed to ‘Two Weekly’ which is what I really want. How do I get the description in my list?

This only appears to get the descriptor for the one Selected. I want to build the list up to have all the descriptors. The example only works because meminfo has been got for a specific item. I thought after loading the list I might get away with:

model.PayPeriods.ForEach(m => m.ToString().Replace("_", " "));

as a first pass since this is the main gripe for needing the descriptors, but the replace get’s ignored.

Yes, replacing underscore with whitespace should solve the problem, as long as, there are no underscores in the description field, as in such case, even the actual underscrores will also be replaced with spaces.

You may have a look at this similar discussion with various varients of code to return list or dictionary and discussions around what if there are multiple attributes, and what should the code do if the description attribute doesn’t exist etc:

http://stackoverflow.com/questions/16886266/how-to-get-all-descriptions-of-enum-values-with-reflection

Use reflection to retrieve the Description attribute.

static void Main(string[] args)
{
    FieldInfo[] info = typeof(PayPeriodTypeList).GetFields();
    foreach (FieldInfo i in info)
    {
        object[] attrs = i.GetCustomAttributes(true); 
        foreach (object attr in attrs)
        {
            if (attr is DescriptionAttribute)
            {
                DescriptionAttribute a = (DescriptionAttribute)attr;
                System.Console.WriteLine("{0}", a.Description);
            }
        }
    }
}

thanks this works for this particular enum. But it has occurred to me that  I probably need a static class as I’m likely to want to do the same with other enums. I’ll see how I get on converting this to a static class and passing an enum.

You could argue that if you need access to the text description then you shouldn’t be using enums in the first place but normal classes.

billcrawley

thanks this works for this particular enum. But it has occurred to me that  I probably need a static class as I’m likely to want to do the same with other enums. I’ll see how I get on converting this to a static class and passing an enum.

Use generics then… The code was just an example of how to get to the attributes.   The rest is up to you.

public static void PrintAttributes<EnumType, AttributeType>()
{
    FieldInfo[] info = typeof(EnumType).GetFields();
    foreach (FieldInfo i in info)
    {
        object[] attrs = i.GetCustomAttributes(true);
        foreach (object attr in attrs)
        {
            if (attr is AttributeType)
            {
                DescriptionAttribute a = (DescriptionAttribute)attr;
                System.Console.WriteLine("{0}", a.Description);
            }
        }
    }
}

To call the static method.

PrintAttributes<PayPeriodTypeList, DescriptionAttribute>();

Hope the below piece meet your requirement.

public enum PayPeriodTypeList
    {
        Undefined = 0,
        [Display(Description = "Monthly")]
        Monthly = 1,
        [Display(Description = "Weekly")]
        Weekly = 3,
        [Display(Description = "Two Weekly")]
        Two_weekly = 4,
        [Display(Description = "Four Weekly")]
        Four_weekly = 5
}

public class MyViewModel
{
               
    public IEnumerable PayPeriod 
    {
        get
        {
            var myList = from PayPeriodTypeList e in Enum.GetValues(typeof(PayPeriodTypeList))
                            select new { Id = (int)e, Name = e.GetDescription() };
 
            return myList;
        }
    }
 
    ……rest of model
}

In the view, you can use Model.PayPeriod.

Leave a Reply