Enum.GetDescription() extension method
July 12th, 2009
I really like extension methods, they allow us to extend the functionality of any CLR type, in this post I will share with you an extension method to get the descriptions for an enum value.
The first thing to do is to use the attribute Description placed on the System.ComponentModel namespace. This attribute is the right one to specify descriptions.
Using this attribute we will decorate our enums items, writing the description for each one.
Now with a little help of reflection we will create an extension method to get this attribute and its description:
Note that if this method do not find a DescriptionAttribute it will return the ToString() result.
That’s all, just be sure that you are including the namespace of the extension method.
You can download the source code here.
Hope be useful!