Click here to Skip to main content
11,647,659 members (55,013 online)
Click here to Skip to main content

Tagged as

TIP: Descriptions for enum values

, 24 Sep 2010 CPOL 1.9K 3
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.

You may also be interested in...

Comments and Discussions

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