2

Is it possible to use an enum with expressions to reflect on the enum values? Consider this hypothetical routine:

public enum Fruit
{
  Apple,
  Pear
}

public void Foo(Fruit fruit)
{
  Foo<Fruit>(() => fruit);
}

public void Foo<T>(Expression<Func<T>> expression)
{
    //... example: work with Fruit.Pear and reflect on it
}

Bar() will give me information about the enum, but I want to work with the actual value.

Background: I've been adding some helper methods to return CustomAttribute information for types and wondered if a similar routine could be used for enums.

I'm fully aware you can work with the enum type to get the CustomAttributes that way.

Update:

I use a similar concept in MVC with helper extensions:

public class HtmlHelper<TModel> : System.Web.Mvc.HtmlHelper<TModel>
{
    public void BeginLabelFor<TProperty>(Expression<Func<TModel, TProperty>> expression)
    {
        string name = ExpressionHelper.GetExpressionText(expression);
    }
}

In this example name would be the member name of the model. I want to do a similar thing with enums, so name would be the enum 'member'. Is this even possible?

Updated example:

public enum Fruit
{
  [Description("I am a pear")]
  Pear
}

public void ARoutine(Fruit fruit)
{
  GetEnumDescription(() => fruit); // returns "I am a pear"
}

public string GetEnumDescription<T>(/* what would this be in a form of expression? Expression<T>? */)
{
  MemberInfo memberInfo;
  // a routine to get the MemberInfo(?) 'Pear' from Fruit - is this even possible?

  if (memberInfo != null)
  {
    return memberInfo.GetCustomAttribute<DescriptionAttribute>().Description;
  }

  return null; // not found or no description
}
Phil Cooper
  • 3,083
  • 39
  • 63
  • 8
    Did you mean to make Foo call Bar? Why are you using expression trees here? What are you actually trying to achieve? Your question is very vague at the moment. – Jon Skeet Aug 23 '13 at 13:44
  • @JonSkeet Sorry, a case of changing before posting. I've updated it and also added an example with a routine used for Mvc where the name of the property is extracted for a label. – Phil Cooper Aug 23 '13 at 13:56
  • 2
    It's still not clear why you don't just call `ToString()` which will give you the name associated with the value. – Jon Skeet Aug 23 '13 at 13:58
  • I'm not interested in the enum name, I was wondering if you could use the same expression technique to retrieve the enum attribute. [This question](http://stackoverflow.com/questions/5097766/how-to-get-custom-attribute-values-for-enums) describes using reflection how to get a custom attribute for an enum value. Considering this approach touches on MemberInfo - is it not possible to use an expression with an enum to get the MemberInfo of the value and get the attributes that way? – Phil Cooper Aug 23 '13 at 14:10
  • 1
    What do you mean by "the enum attribute"? You said "name would be the enum 'member'" - assuming you mean "Pear" or "Apple", that's exactly what you get from `ToString`. If that's not what you want, you really need to give a full example of how you intend to call `Foo(Fruit)` and what you'd expect the result to be. – Jon Skeet Aug 23 '13 at 14:14
  • is this what you are looking for? http://weblogs.asp.net/grantbarrington/archive/2009/01/19/enumhelper-getting-a-friendly-description-from-an-enum.aspx – thumbmunkeys Aug 23 '13 at 18:11

1 Answers1

5

You don't need Expressions for this. All you need to know is that enums have a field for each of their values. This means you can do something like:

public static string GetEnumDescription<T>(T enumValue) where T : struct, Enum
{
    FieldInfo field = typeof(T).GetField(enumValue.ToString());

    if (field != null)
    {
        var attribute = field.GetCustomAttribute<DescriptionAttribute>();

        if (attribute != null)
            return attribute.Description;
    }

    return null; // not found or no description
}
svick
  • 236,525
  • 50
  • 385
  • 514