tags:

views:

173

answers:

2

I have an enum representing all material assembly codes in the system:

public enum EAssemblyUnit
{
    [Description("UCAL1")]
    eUCAL1,
    [Description("UCAL1-3CP")]
    eUCAL13CP,
    [Description("UCAL40-3CP")]
    eUCAL403CP, // ...
}

In legacy code in another part of the system, I have objects labeled with strings that match the enum descriptions. Given one of those strings, what's the cleanest way to get the enum value? I envision something like:

public EAssemblyUnit FromDescription(string AU)
{
    EAssemblyUnit eAU = <value we find with description matching AU>
    return eAU;
}
+4  A: 

You'd need to iterate through all the static fields of the enum (that's how they're stored in reflection) finding the Description attribute for each one... when you spot the right one, get the value of the field.

For example:

public static T GetValue<T>(string description)
{
    foreach (var field in typeof(T).GetFields())
    {
        var descriptions = (DescriptionAttribute[]) 
               field.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (descriptions.Any(x => x.Description == description))
        {
            return (T) field.GetValue(null);
        }
    }
    throw new SomeException("Description not found");
}

(This is generic just to make it reusable for different enums.)

Obviously you'd want to cache the descriptions if you're doing this even slightly frequently, e.g.:

public static class DescriptionDictionary<T> where T : struct
{
    private static readonly Dictionary<string, T> Map = 
        new Dictionary<string, T>();

    static DescriptionDictionary()
    {
        if (typeof(T).BaseType != typeof(Enum))
        {
            throw new ArgumentException("Must only use with enums");
        }
        // Could do this with a LINQ query, admittedly...
        foreach (var field in typeof(T).GetFields
                 (BindingFlags.Public | BindingFlags.Static))
        {
            T value = (T) field.GetValue(null);
            foreach (var description in (DescriptionAttribute[]) 
               field.GetCustomAttributes(typeof(DescriptionAttribute), false))
            {
                // TODO: Decide what to do if a description comes up
                // more than once
                Map[description.Description] = value;
            }
        }
    }

    public static T GetValue(string description)
    {
        T ret;
        if (Map.TryGetValue(description, out ret))
        {
            return ret;
        }
        throw new WhateverException("Description not found");
    }
}
Jon Skeet
Ugh, was really hoping for "here's a handy function that acts like a Dictionary<> between description and value". Maybe I'll build a class that holds that dictionary statically and populates it the first time it's needed...?
Chuck Wilbur
Yes - the core bit you'll need is the bit of my answer which gives the description. Oh stuff it... I'll write it now. Hang on :)
Jon Skeet
Yep, that's what I was thinking, but I'll be happy to take code written by Jon Skeet in 5 minutes over what I would write myself in probably more like an hour. Tested. Works like a charm.
Chuck Wilbur
P.S. Heck, I learned 2 or 3 new things about C# I didn't know just reading that class.
Chuck Wilbur
A: 
public EAssemblyUnit FromDescription(string AU)
{
    EAssemblyUnit eAU = Enum.Parse(typeof(EAssemblyUnit), AU, true);
    return eAU;
}
Chris McCall
That will do it for the *name*, not the description attribute.
Jon Skeet
I'll get you next time, Jon Skeet! *shakes tiny fist at cloud*
Chris McCall