User Id :    Password :      New Member   Forgot Password  
 
Extending Enum to return attached string
Description No of time there is requirement of getting string of Enum value to display purpose or to perform other task. So to deal with this requirement I wrote one extension function that get the string attached with the enum value.   No. of Views     1699
  Rating     0
Author Pranay Rana   Posted On     30 Jul 2012
Tags ASP.NET,C#,C# 4.0,ASP.NET : How To,C# : How To    

Sample Code   Download Code

No of time there is requirement of getting string of Enum value to display purpose or to perform other task. So to deal with this requirement I wrote one extension function that get the string attached with the enum value.

Note : here attached string value is the attribute that is attached with each enum value that you are creating in enum variable.


Following is extension function to get string attached with enum.

 

public static class MyExtensions
{
   public static string GetEnumDescription(this Enum value)
   {           
         FieldInfo fi = value.GetType().GetField(value.ToString());
 
         object[] attributes = fi.GetCustomAttributes(true);
 
         if (attributes != null &&
            attributes.Length > 0)
                return ((DescriptionAttribute) attributes[0]).Description;
         else
                return value.ToString();
    }
}

 

 

The code above is making use of the reflection feature of .net framework. With the help of reflection it first get the information about the field and than get the attribute attached with that field. Attribute attached with enum value is DescriptionAttribute so code convert attribute object to DescriptionAttribute and return string attached with it. If there is no attribute attached with enum value it returns the interger value of enum as string.



Note : Attribute is metadata attached with the type you can attache metadata with class, function , property etc. Read More: Attributes (C# Programming Guide)


Following is simple program that is making use of extension function and to display the string attached with the enum value.

 

class Program
    {
        public enum WeekDay
        {
            [Description("Monday")]
            Monday,
            [Description("Tuesday")]
            Tuesday
        }
 
        static void Main(string[] args)
        {
            string str = (WeekDay.Monday).GetEnumDescription();
            Console.WriteLine(str);
            Console.ReadLine();
        }
    }

 

 

In above code there is enum create with the name WeekDay and as per the requirement it require to attache attribute with the each enum value to function to work properly if no attribute attached with enum value than function return value of enum i.e interger value associated with the enum.


So when code in "Main function" get executed call to function "GetEnumDescription" it return "Monday" as output on the console window which is associated with enum value.

 

About Author

About Author Hey, I am Pranay Rana, working as a Senior Software engineer. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5 years now.
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
You can visit me on my blog - http://pranayamr.blogspot.com/
Pranay Rana
No Photo
 
Country India
Company GMind Solutions
Home Page http://pranayamr.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code