Benefits

Instead of having constants and huge arrays all over the code, the above solution keeps declarations and descriptions nicely together.

History

I've only found out by accident that the framework already provides a DescriptionAttribute class.

I built the first version with my own custom attribute and got conflicts when I started using the ComponentModel namespace.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

I have created a small prototype using this attribute and trying to improve its performance using different caching methods. This code is available on my blog (in french, but you can download and understand the code in english). The cached results are up to 85 times faster than uncached results, depending on the caching mechanism.

People are just notoriously impossible. --DavidCrowThere's NO excuse for not commenting your code. -- John Simmons / outlaw programmerPeople who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith

This method attempts to break an enum down into its greatest common values. The return value indicates if the enum was completely covered by the returned enum value list. Perhaps it should be called TryGetEnumValues

I've just written a nice console application that uses reflection to analyse any assembly for enumerated types for description attributes. It generates a minimal XML documentation set with the descriptions used as the summaries for each enumeration. This is really useful for keeping your documentation of error codes, descriptors in sync with your source code -> if you use NANT & NDoc.

It also can accept pre-generated XML doc files and insert the summaries into the correct member positions.

I liked this. Great idea!! Here it is going an idea on the message posted by Uwe Keim. The attribute could contain a property that would be destined to the name of resource (e.g. "AnyLib.Resources.Common.Yes"). In moment of solving string (GetDescription method) the mechanism could verify this property, if search be filled out the resource otherwise it uses the default description (hardcoded).

I think you need to embellish us with a more detailed look at enumerations with spaces. I understand what you are saying about invalid characters, but the example isn't quite clear on it. Especially if you have an XML enumeration that you would be using in a web service environment.

In a web service environment, if the service uses a string enumeration VS. compacts the names for the enumeration and lays on the XMLAttribute tag with the actual name for the value which contained spaces.

They are two different things if I understand your message correctly. Just like property name and property description are two different attributes of a property.

Enum name is more useful for developers and compilers. It is not practically useful for display to end user since it has all the naming limits and is not localizable.

Just like the other comment from other thread, it would be nice to have this attribute to use resources instead of hard coded string. Also performance would be a concern since it uses reflection alot. How about caching the result from GetDescription static method?

Yes, it is. Derive from DescriptionAttribute and override the Description property. Microsoft does this throughout their code, including an SR (localized resources) class with a set of SRCategoryAttribute and SRDescriptionAttribute classes. (CategoryAttributes is localized a little differently).