/// <summary>
/// 获取枚举值的描述,如果没有描述,则返回枚举名称
/// </summary>
/// <param name="en">枚举值</param>
/// <returns>描述</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();//定义一个集合
foreach (int i in Enum.GetValues(typeof(ErrorCode))) //将枚举的描述和value循环添加至集合中(ErrorCode:枚举的名称)
{
ListItem listitem = new ListItem(GetEnumDescription((ErrorCode)i), i.ToString());
// ListItem listitem = new ListItem(Enum.GetName(typeof(EnumActivityType), i), i.ToString());//获取枚举名称集合
listcode.Add(listitem);
}
-----------------------------------------------------------------------------------------------------------------------------
for (int i = 0; i < listcode.Count; i++) // 循环就可输出每个枚举的描述和value
{
ListItem item = (ListItem)listcode[i];
var d = item.Text;
var b = item.Value;
}
------------------------------------------------看后点推荐 越点越有劲-----------------------------------------------------