0

Good morning. I have the need to create a little helper class, using generics, however my knowledge of generics is very low. So here is what i need. i have defined enums in C#, to have description properties. for example

public enum EnumLineItemErrorCode
{
    [Description("None")]
    None = 0,
    [Description("helpful Desc")]
    MissnigA= 1,
    [Description("another desc")]
    MissingB = 2
}

I also have created helper functions that get he description out of the enums like

public static string GetEnumDescription(Enum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());
    DescriptionAttribute[] attributes =
        (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
    return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}

Now if i need the desc i can call the method like the following

EnumerationsHelper.GetEnumDescription(EnumLineItemErrorCode.MissnigA);

However when we want to do bindinds to a datasource i currently do the following for each value on the enum.

dropdownList.Add(new ListItem(EnumerationsHelper.GetEnumDescription(EnumLineItemErrorCode.MissnigA), EnumLineItemErrorCode.MissnigA.ToString()));

But this approach is inflexible as the enum grows in size and also because it does not automatically adds a value to the list if i just add it to the enum.

So my question is.

Can u create a helper method that will return me a list of Description, Value, where description is the enum description and the value is the enum internal value. For example on the code that i have will be used as

object t EnumerationsHelper.GetDescriptionAndValuesAslist(EnumLineItemErrorCode);

and object t is structure of with values <"None",None">,<"MissnigA","helpful Desc">,<"MissingB","another desc">

Thank you in advance.

3 Answers 3

1

This will do the job.

private string[] GetEnumDescriptions<T>()
{
    return Enum.GetValues(typeof(T))
               .Cast<Enum>()
               .Select(GetEnumDescription)
               .ToArray();
}

Use case:

var descriptions = GetDescriptions<EnumLineItemErrorCode>();

Note: If T is not of type Enum Enum.GetValues method will throw exception.

Sign up to request clarification or add additional context in comments.

Comments

0

I quickly wrote the following helper method for you

public static IDictionary<string, string> GetEnumBindings<T>()
{
    if (!typeof(Enum).IsAssignableFrom(typeof(T)))
    {
        throw new ArgumentException("The provided type is not an enum");
    }

    var result = new Dictionary<string, string>();
    var fieldNames = Enum.GetNames(typeof (T));

    foreach (var fieldName in fieldNames)
    {
        var fieldAttributes = typeof (T).GetField(fieldName)
                                       .GetCustomAttributes(typeof (DescriptionAttribute), false);

        var description = fieldAttributes.Any()
                              ? ((DescriptionAttribute) fieldAttributes.First()).Description
                              : fieldName;

        result.Add(fieldName, description);
    }

    return result;
}

And use case is:

var bindings = GetEnumBindings<EnumLineItemErrorCode>();
var listItems = bindings.Select(b => new ListItem(b.Value, b.Key));

After that just add listItems to your DropDownList. Good luck!

UPD: Since description is not unique, I modified code a little bit to add field name as a key to the dictionary.

1 Comment

This solution provided the actual desired output, Thanks so much, the other one were good starting places, but this one was the complete package
0

This is what you are looking for: Enum.GetValues

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.