using System.ComponentModel; using System.Reflection; /// /// Contains an enumerated constant value and a friendly description of that value, if one exists. /// /// /// The enumerated type of the value. /// public class EnumDescriptor { /// /// The friendly description of the value. /// private string _description; /// /// The enumerated constant value. /// private T _value; /// /// Gets the friendly description of the value. /// /// /// A String containing the value's Description attribute value if one exists; otherwise, the value name. /// public string Description { get { return this._description; } set { this._description = value; } } /// /// Gets the enumerated constant value. /// /// /// An enumerated constant of the EnumDescriptor's generic parameter type. /// public T Value { get { return this._value; } set { this._value = value; } } /// /// Creates a new instance of the EnumDescriptor class. /// /// /// The value to provide a description for. /// public EnumDescriptor(T value) { this._value = value; // Get the Description attribute. FieldInfo field = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false); // Use the Description attribte if one exists, otherwise use the value itself as the description. this._description = attributes.Length == 0 ? value.ToString() : attributes[0].Description; } /// /// Overridden. Creates a string representation of the object. /// /// /// The friendly description of the value. /// public override string ToString() { return this._description; } }