.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Function to return description of enum type

Posted By:Rajshree       Posted Date: October 22, 2010    Points: 15    Category: Visual Studio    URL: http://www.dotnetspark.com  

Function to return description of enum type. Returns descriptions (if available) or enum names (if not) for all values in the specified Enum.
 

 public static string[] GetDescriptions(Type enumType)
        {
            List values = new List();
            FieldInfo[] fieldInfos = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attributes.Length > 0)
                {
                    values.Add(attributes[0].Description);
                }
                else
                {
                    values.Add(fieldInfo.GetValue(fieldInfo.Name).ToString());
                }
            }
            return values.ToArray();
        }


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend