Mar 20
2008

The technique of having description attributes against enum values is something I've been doing for a few years. However I just had a requirement to use this technique again, and thought it would be useful as an extension method.

   1: public enum ITEMUPDATESTATUS
   2: {
   3:     [Description("Unknown")]
   4:     Unknown = 0,
   5:     [Description("Failed validation")]
   6:     FailedValidation,
   7:     [Description("Product not found")]
   8:     SKUNotFound,
   9:     [Description("Manufacturer code doesn't exist")]
  10:     ManufacturerCodeNotExists
  11: }
  12:  
  13: public static class Enum<T>
  14: {
  15:     public static string Description(T value)
  16:     {
  17:         DescriptionAttribute[] da = (DescriptionAttribute[])(typeof(T).GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false));
  18:         return da.Length > 0 ? da[0].Description : value.ToString();
  19:     }
  20: }

And then the description can be obtained by a simple syntax:

   1: ITEMUPDATESTATUS i = ITEMUPDATESTATUS.FailedValidation;
   2: Console.WriteLine(Enum<ITEMUPDATESTATUS>.Description(i));



mike posted on March 20, 2008 12:56

Comments

Posted on Thursday, 1 May 2008 12:51
That's neat; if you'd store the strings in a resource file and not hard-code them into the attributes it would be even better ;)
Posted on Thursday, 1 May 2008 19:25
Pingback from comsa-soho.dyndns.info

Enum description values
Posted on Monday, 12 May 2008 18:44
Sweet use of an extension method.  Except what you're doing isn't an extension method; its more of a wrapper.

Your code as an extension method would be:

    public static class Extensions
    {
        public static string Description(this Enum e)
        {            
            DescriptionAttribute[] da = (DescriptionAttribute[])(e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false));
            return da.Length > 0 ? da[0].Description : e.ToString();
        }
    }

This works great.  Its definitely going in my toolbox.  Thanks!
Posted on Monday, 19 May 2008 19:00
You probably should in some type checking to make sure that T is actually an Enum and that T is not null. The other functionality this method would really benefit from would be handling of nullable enums also. Personally I think it's best practice in C# to always make value object types nullable such as currency numerics, datetimes, most bools, and all enums. Unfortunately prior to 3.5 nullable strings can't be included in that.
Sam
Posted on Monday, 19 May 2008 22:00
Very neat--I wish there was a way you could avoid the Enum<T> generic, though--it seems to add complexity
jv
Posted on Tuesday, 20 May 2008 11:17
Your solution isn't an extension method.
Posted on Tuesday, 20 May 2008 16:35
Pingback from alvinashcraft.com

Dew Drop - May 20, 2008 | Alvin Ashcraft's Morning Dew
Posted on Tuesday, 20 May 2008 18:19
Matt Schuette took this a step further in a comment on my blog...

blogs.freshlogicstudios.com/.../View.aspx

bobsmith
Posted on Tuesday, 20 May 2008 19:46
Nice idea.  However, your implementation is as a Helper Class, not an Extension Method.
Cherian
Posted on Tuesday, 3 June 2008 08:40
Remove line numbers from your code section or implement copy functionality please.
Cherian
Posted on Tuesday, 3 June 2008 08:47
I still dont get how this works.How can you add a description to the enum without implementing a enum attribute class
Naz
Posted on Tuesday, 3 June 2008 16:42
I've done this using extension methods you can check it out here.

www.objectreference.net/.../...tension-method.aspx
Posted on Thursday, 31 July 2008 08:40
Very useful, thanks for posting!  I am in a situation where I needed this exact solution.  We speak VB at my shop so I went ahead and converted your code.  Works like a charm!
Posted on Wednesday, 3 September 2008 21:09
Don't forget to add:

using System.ComponentModel;


Posted on Saturday, 20 September 2008 06:58
Trackback from Kevin Pang

Mapping Enums To Strings and Strings to Enums in .NET
Posted on Sunday, 21 September 2008 15:09
I expanded on your example to support the reverse direction (mapping the string back to the enum).  You can check it out in my blog post here:


www.kevinwilliampang.com/.../...-Enums-in-NET.aspx
Posted on Monday, 15 June 2009 11:44
Pingback from rapid-dev.net

Mapping Enums to Strings and Strings to Enums in .NET | rapid-DEV.net

Add comment




  Country flag

biuquote
Loading