Click here to Skip to main content
12,698,775 members (23,551 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.5K views
3 bookmarked
Posted

TIP: Descriptions for enum values

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

Pro
Pro

Comments and Discussions

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