2
\$\begingroup\$

The Enumeration in question is based on int, so the following Extension returns a Dictionary<int, string> of the Enum's Value and Description.

Is there a cleaner way of implementing this?

public enum TeamDepartments
{
    [Description("Geschäftsleitung")] Management = 1,
    [Description("Verkaufsleitung")] Sales = 2
}

public static class UtilityExtensions
{
    public static Dictionary<int, string> ToDictionary(this Type EnumerationType)
    {
        Dictionary<int, string> RetVal = new Dictionary<int,string>();
        var AllItems = Enum.GetValues(EnumerationType);
        foreach (var CurrentItem in AllItems)
        {
            DescriptionAttribute[] DescAttribute = (DescriptionAttribute[])EnumerationType.GetField(CurrentItem.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
            string Description = DescAttribute.Length > 0 ? DescAttribute[0].Description : CurrentItem.ToString();
            RetVal.Add((int)CurrentItem, Description);
        }
        return RetVal;
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ The method name should be GetDescription. Also, the dictionary is unnatural because you lose some of the type-safety, plus you are not wasting THAT much time if you call GetDescription every time. If you do want to keep the dictionary anyway, then I would at least wrap access to it into a method with a tighter signature. \$\endgroup\$
    – Leonid
    May 31 '12 at 19:40
  • \$\begingroup\$ The larger context for this Extension is binding an Enum to an ASP.net dropdown, which now occurs to me can be refactored into a single method. \$\endgroup\$
    – kalyfe
    Jun 1 '12 at 4:49
  • \$\begingroup\$ I have often dealt with a situation where I need to display something in a combo box, something that does not change very often, something that does not have too many possible values, but also something that comes from a database. My situation is very similar to: stackoverflow.com/questions/5886394/… At the end of it I decided that it is better to not use Enums, but have a small object that overrides a ToString for display purposes but also contains a bunch of other useful info. \$\endgroup\$
    – Leonid
    Jun 1 '12 at 15:24
1
\$\begingroup\$

I have this class in my code which is very similar to what you are looking for:

public static class EnumHelper {
    public static string GetRealName(this Enum value) {
        if (value == null) {
            throw new ArgumentException("value");
        }

        string name = value.ToString();
        var fieldInfo = value.GetType().GetField(name);
        if (fieldInfo == null) return "";
        var attributes = (RealNameAttribute[])fieldInfo.GetCustomAttributes(typeof(RealNameAttribute), false);

        if (attributes.Length > 0) {
            name = attributes[0].Name;
        }

        return name;
    }
    public static bool IsIgnored(this Enum value) {
        if (value == null) {
            throw new ArgumentException("value");
        }

        string name = value.ToString();
        var fieldInfo = value.GetType().GetField(name);

        return fieldInfo == null || fieldInfo.IsDefined(typeof(IgnoreAttribute), true);
    }

    public static Dictionary<int, string> ToDictionary(this Type enumType) { 
        return Enum.GetValues(enumType)
            .Cast<object>()
            .Where(f => !((Enum)f).IsIgnored())
            .ToDictionary(k => (int)k, v => ((Enum)v).GetRealName()); 
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ I adjusted your code to fit my needs and I'll update the question with it -> answer accepted. \$\endgroup\$
    – kalyfe
    May 31 '12 at 14:00
0
\$\begingroup\$

You should separate the code returning the attributes of a type into another extension method. It would probably prove useful later on, anyway. Perhaps return an IEnumerable<> of attributes, and process it into a dictionary using a LINQ statement.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.