Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

TIP: Descriptions for enum values

, 24 Sep 2010
Rate this:
Please Sign up or sign in to vote.
You could also make an extension method...public static class EnumExtensions{ public static string Description(this Enum e) { return e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[])[0].Description; ...
You could also make an extension method...
 
public static class EnumExtensions
{
    public static string Description(this Enum e)
    {
        return e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[])[0].Description;
    }
}
 
Then the code to use is as simple as:
 
public enum SampleEnum
{
    [Description("Value 1")]
    One,
    [Description("Value 2")]
    Two,
    [Description("Value 3")]
    Three
}
 
SampleEnum val = SampleEnum.One;
 
string desc = val.Description();
 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ron Beyer
President 6D Systems LLC
United States United States
I studied Software Engineering at Milwaukee School of Engineering for 2 years before switching to Management of Information Systems for a more business oriented approach. I've been developing software since the age of 14, and have waded through languages such as QBasic, TrueBasic, C, C++, Java, VB6, VB.NET, C#, etc. I've been developing professionally since 2002 in .NET.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 24 Sep 2010
Article Copyright 2010 by Ron Beyer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid