Attribute is a method of associating additional data to an Attribute (and other constructor), while enumeration is the most commonly used structure in programming, enumeration is essentially a constant value. Compared to using these values directly, enumeration provides us with better readability. We know that the basic types of enumeration can only be values (byte, sbyte, short, ushort, int, uint, long, or ulong). In general, enumeration can meet our needs, however, sometimes we need to add more information to the enumeration, but it is not enough to only use these value types. In this case, we can apply the features of the enumeration type to make the enumeration more information.
Use DescriptionAttribute in Enumeration
First, introduce: namespace. The following is an enumeration application feature:
[Description( [Description( [Description( }
The following is an extension method for obtaining the Description feature:
GetDescription<TEnum>( TEnum : Type type = (! ArgumentException(, MemberInfo[] memberInfo = (memberInfo != && memberInfo.Length > [] attrs = memberInfo[].GetCustomAttributes((DescriptionAttribute), (attrs != && attrs.Length > ((DescriptionAttribute)attrs[ }
Finally, we can use this extension method to obtain the descriptive information of this enumeration:
Main( description = }