250

Possible Duplicate:
Finding an enum value by its Description Attribute

I have a generic extension method which gets the Description attribute from an Enum:

enum Animal
{
    [Description("")]
    NotSet = 0,

    [Description("Giant Panda")]
    GiantPanda = 1,

    [Description("Lesser Spotted Anteater")]
    LesserSpottedAnteater = 2
}

public static string GetDescription(this Enum value)
{            
    FieldInfo field = value.GetType().GetField(value.ToString());

    DescriptionAttribute attribute
            = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
                as DescriptionAttribute;

    return attribute == null ? value.ToString() : attribute.Description;
}

so I can do...

string myAnimal = Animal.GiantPanda.GetDescription(); // = "Giant Panda"

now, I'm trying to work out the equivalent function in the other direction, something like...

Animal a = (Animal)Enum.GetValueFromDescription("Giant Panda", typeof(Animal));
Community
  • 1
  • 1
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253

6 Answers6

369
public static class EnumEx
{
    public static T GetValueFromDescription<T>(string description) where T : Enum
    {
        foreach(var field in typeof(T).GetFields())
        {
            if (Attribute.GetCustomAttribute(field,
            typeof(DescriptionAttribute)) is DescriptionAttribute attribute)
            {
                if (attribute.Description == description)
                    return (T)field.GetValue(null);
            }
            else
            {
                if (field.Name == description)
                    return (T)field.GetValue(null);
            }
        }

        throw new ArgumentException("Not found.", nameof(description));
        // Or return default(T);
    }
}

Usage:

var panda = EnumEx.GetValueFromDescription<Animal>("Giant Panda");
Neo
  • 4,145
  • 6
  • 53
  • 76
max
  • 33,369
  • 7
  • 73
  • 84
  • 58
    If you add the "this" keyword in front of string ... public static T GetValueFromDescription(this string description) ... it becomes an extension method and you can use syntax like: var x = "Giant Panda".GetValueFromDescription(); – Keith G Feb 29 '12 at 18:16
  • How does one find if the description is not valid? with: `var x = EnumEx.GetValueFromDescription("Dinosaur");` I know it would throw an exception. But what would x contain? – Ren Jun 10 '13 at 19:07
  • @VenkatRenukaPrasad if it would throw anyway, then `x` cant have anything and you need not worry. Have an exception handling mechanism and handle it. – nawfal Jun 11 '13 at 01:22
  • @ErikE `Enum` cannot be specified as a constraint (https://msdn.microsoft.com/en-us/library/56b2hk61.aspx) – max Jul 22 '15 at 23:36
  • Code is great but forgot to mention that you need to include `using System.ComponentModel;` – vbguyny May 18 '16 at 19:38
  • I used the same method for me, but as mentioned by @Keith as an extension method. It Worked – Atta H. Aug 23 '17 at 20:36
  • 1
    Might want to do case insensitive comparisons instead like this. if (string.Equals(attribute.Description,description, StringComparison.InvariantCultureIgnoreCase)) return (T)field.GetValue(null); It caught me out. – Damien Sawyer Feb 25 '18 at 23:18
  • I've posted how you can convert this to the new c# 7.3 `Enum` type constraint here https://stackoverflow.com/questions/2787506/get-enum-from-enum-attribute/50336830#50336830 – DLeh May 14 '18 at 18:32
  • perfect.. bravo!!! – Prince Tegaton Jun 27 '18 at 08:33
  • 1
    To make the above code work better, change the line "foreach(var field in type.GetFields())" to "foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static))". Otherwise in the else below, the name of the field is 'value__' instead of the real name. – Dennis Mar 15 '19 at 08:28
46

rather than extension methods, just try a couple of static methods

public static class Utility
{
    public static string GetDescriptionFromEnumValue(Enum value)
    {
        DescriptionAttribute attribute = value.GetType()
            .GetField(value.ToString())
            .GetCustomAttributes(typeof (DescriptionAttribute), false)
            .SingleOrDefault() as DescriptionAttribute;
        return attribute == null ? value.ToString() : attribute.Description;
    }

    public static T GetEnumValueFromDescription<T>(string description)
    {
        var type = typeof(T);
        if (!type.IsEnum)
            throw new ArgumentException();
        FieldInfo[] fields = type.GetFields();
        var field = fields
                        .SelectMany(f => f.GetCustomAttributes(
                            typeof(DescriptionAttribute), false), (
                                f, a) => new { Field = f, Att = a })
                        .Where(a => ((DescriptionAttribute)a.Att)
                            .Description == description).SingleOrDefault();
        return field == null ? default(T) : (T)field.Field.GetRawConstantValue();
    }
}

and use here

var result1 = Utility.GetDescriptionFromEnumValue(
    Animal.GiantPanda);
var result2 = Utility.GetEnumValueFromDescription<Animal>(
    "Lesser Spotted Anteater");
Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
  • `value.ToString()` is expensive, do not call it twice (.. if it matters)... – nawfal Jun 11 '13 at 01:26
  • @nawfal, expensive compared to what? – MEMark Oct 16 '14 at 15:24
  • @MEMark I dont remember what I was thinking then, but may be [this](http://stackoverflow.com/a/17034624/661933) – nawfal Oct 16 '14 at 16:21
  • 1
    If you change the method to add `where T : Enum` just before the open curly brace, you won't have to check the `typeof(T)` at runtime. – ErikE Jul 20 '15 at 16:52
  • @ErikE you can't do that in C#. – ATD Dec 17 '15 at 19:52
  • 1
    @ATD You're right, and I had forgotten that at the time. However, it is supported in the framework, so you can use [Fody.ExtraConstraints](https://github.com/Fody/ExtraConstraints/blob/master/README.md) to get the same effect, fully compiled and working as if `where T : Enum` had been specified. The syntax is `public void MethodWithEnumConstraint<[EnumConstraint] T>() {...}` which gets compiled to `public void MethodWithEnumConstraint() where T: struct, Enum {...}`. – ErikE Dec 18 '15 at 18:11
18

The solution works good except if you have a Web Service.

You would need to do the Following as the Description Attribute is not serializable.

[DataContract]
public enum ControlSelectionType
{
    [EnumMember(Value = "Not Applicable")]
    NotApplicable = 1,
    [EnumMember(Value = "Single Select Radio Buttons")]
    SingleSelectRadioButtons = 2,
    [EnumMember(Value = "Completely Different Display Text")]
    SingleSelectDropDownList = 3,
}

public static string GetDescriptionFromEnumValue(Enum value)
{
        EnumMemberAttribute attribute = value.GetType()
            .GetField(value.ToString())
            .GetCustomAttributes(typeof(EnumMemberAttribute), false)
            .SingleOrDefault() as EnumMemberAttribute;
        return attribute == null ? value.ToString() : attribute.Value;
}
Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188
Theo Koekemoer
  • 228
  • 2
  • 5
6

Should be pretty straightforward, its just the reverse of your previous method;

public static int GetEnumFromDescription(string description, Type enumType)
{
    foreach (var field in enumType.GetFields())
    {
        DescriptionAttribute attribute
            = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))as DescriptionAttribute;
        if(attribute == null)
            continue;
        if(attribute.Description == description)
        {
            return (int) field.GetValue(null);
        }
    }
    return 0;
}

Usage:

Console.WriteLine((Animal)GetEnumFromDescription("Giant Panda",typeof(Animal)));
Jamiec
  • 133,658
  • 13
  • 134
  • 193
3

You can't extend Enum as it's a static class. You can only extend instances of a type. With this in mind, you're going to have to create a static method yourself to do this; the following should work when combined with your existing method GetDescription:

public static class EnumHelper
{
    public static T GetEnumFromString<T>(string value)
    {
        if (Enum.IsDefined(typeof(T), value))
        {
            return (T)Enum.Parse(typeof(T), value, true);
        }
        else
        {
            string[] enumNames = Enum.GetNames(typeof(T));
            foreach (string enumName in enumNames)
            {  
                object e = Enum.Parse(typeof(T), enumName);
                if (value == GetDescription((Enum)e))
                {
                    return (T)e;
                }
            }
        }
        throw new ArgumentException("The value '" + value 
            + "' does not match a valid enum name or description.");
    }
}

And the usage of it would be something like this:

Animal giantPanda = EnumHelper.GetEnumFromString<Animal>("Giant Panda");
djdd87
  • 67,346
  • 27
  • 156
  • 195
  • 2
    This is logically wrong (though can work in sensible scenarios). For instance if you have enum like `Animal { ("Giant Panda")GiantPanda, ("GiantPanda")Tiger }` and you call `GetEnumFromString("GiantPanda")`, you will get `GiantPanda` back, but I expect `Tiger` back. Have a function to either read enum string, or its description. Mixing both is a bad API style and can confuse clients. Or may be its good style to prevent user mistakes :) – nawfal Jun 11 '13 at 11:03
1

You need to iterate through all the enum values in Animal and return the value that matches the description you need.

Andrei Pana
  • 4,484
  • 1
  • 26
  • 27