0

I'm looking for a way to lookup an enumeration field by providing it's display name. To lookup the display name I wrote this snipped which returns me the appropriate field (if available) as an arbitary type.

if (!type.IsEnum) throw new ArgumentException("type");
        return (from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
                    where field.IsDefined(typeof(DisplayNameAttribute))
                let attribute = field.GetCustomAttribute(typeof(DisplayNameAttribute)) as DisplayNameAttribute
                    where attribute != null && attribute.DisplayName.Equals(lookup, StringComparison.InvariantCultureIgnoreCase)
                select (T)field.GetValue(null)).FirstOrDefault();

Now, I would like to call it this way:

MyEnum instance = MyEnum.GetFieldByDisplayName("my friendly name");

I tried creating an extension method that takes "this Type" as well as "this Enum" as parameter, but it never shows up on MyEnum. What am I doing wrong?

Atrotygma
  • 1,133
  • 3
  • 17
  • 31
  • You mean `typeof(MyEnum)`? – sq33G Jul 17 '13 at 11:03
  • Just to be sure, your extension method is `public static`, the first parameter is of type `this MyEnum` (or `this Enum`) and is in a static class, that is referenced in the class you want to use that extension method and you're using it on an instance of the Enum and not the Enum itself? – Corak Jul 17 '13 at 11:09
  • ....wait, what exactly is a field of an Enum? – sq33G Jul 17 '13 at 11:20
  • I think you're working too hard. Have you considered [Enum.Parse](http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx)? – sq33G Jul 17 '13 at 11:21
  • @sq33G A field of an enumeration is an entry with a value, either given or autogenerated by the compiler. And Enum.Parse is useless in my case because I want to retrieve an enumeration instance by it's description, not by its name. – Atrotygma Jul 17 '13 at 11:49
  • You mean that you are using reflection to turn your enum into a struct? – sq33G Jul 17 '13 at 12:02

2 Answers2

1

Considering your preferred usage

MyEnum instance = MyEnum.GetFieldByDisplayName("my friendly name");

You are trying to define a static method on an enum type, not an extension method. An extension method appears on an instance of a type, not on the type itself.

If you define an extension type, you can use it like

MyEnum instance = MyEnum.SomeValue.GetFieldByDisplayName("my friendly name");

AFAIK you cannot define a method (or something else) that will allow you to use it as you prefer, since you cannot define a static method on an enum type.

Maarten
  • 22,527
  • 3
  • 47
  • 68
0

Check this out - Enumeration extension methods

To enable the extension method to be shown it is important how you write the prototype of the method, not the method body.

So, you should have something like this:

public static void Something(this Enum e)
{
    // code here
}
Community
  • 1
  • 1
dutzu
  • 3,883
  • 13
  • 19
  • Oh, what you actually want is to see the extension for the Enum type, not for an "instance" – dutzu Jul 17 '13 at 11:05
  • @Atrotygma Then that looks more like a static method of a type not an extension method of a type. If you want to have an extension method, then you will have in the end a method with (this Type) and then call it like typeof(Enum).MyMethod. – dutzu Jul 17 '13 at 11:07
  • @dutzu This does not allow the OP to use it like MyEnum.SomeExtensionMethod(...). – Maarten Jul 17 '13 at 11:09
  • 1
    You should create a static class that has that method you want, as a static method along with a member of the type Enum that you want. Take this as an example: http://msdn.microsoft.com/en-us/library/bb383974.aspx – dutzu Jul 17 '13 at 11:09