<summary>
Gets the description of the enumeration value, and returns the enumeration name if there is no description
</summary>
<param name= "EN" > Enumeration values </param>
<returns> Description </returns>
public static string getenumdescription (Enum en)
{
FieldInfo fi = en. GetType (). GetField (en. ToString ());
object[] Attrs = fi. GetCustomAttributes (typeof (DescriptionAttribute), false);
if (attrs! = null && attrs. Length > 0)
{
Return ((DescriptionAttribute) attrs[0]). Description;
}
Else
{
Return en. ToString ();
}
}
ArrayList Listcode = new ArrayList ();//Define a collection
foreach (int i in Enum.getvalues (typeof (ErrorCode)))//Adds a value loop to the collection (ErrorCode: Name of the enumeration)
{
ListItem ListItem = new ListItem (Getenumdescription ((ErrorCode) i), i.tostring ());
ListItem ListItem = new ListItem (Enum.getname (typeof (Enumactivitytype), i), i.tostring ());//Get Enumeration name Collection
Listcode. ADD (ListItem);
}
for (int i = 0; i < Listcode. Count; i++)///loop to output the description and value of each enumeration (can be used to bind a value to a drop-down list box)
{
ListItem item = (ListItem) listcode[i];
var d = Item. Text;
var b = Item. Value;
}
C # binds all descriptions and value in an enumeration to a drop-down list of methods