枚举类定义
public class EnumItem
{
public string Text { get; set; }
public int Value { get; set; }
public string Description { get; set; }
public EnumItem(string text, int value, string description)
{
Text = text;
Value = value;
Description = description;
}
}
获取枚举文本,值和描述的方法
public static List<EnumItem> GetEnumItems(Type enumType)
{
var list = new List<EnumItem>();
if (enumType.IsEnum != true)
{
throw new InvalidOperationException();
}
var typeDescription = typeof(DescriptionAttribute);
var fields = enumType.GetFields();
foreach (var field in fields)
{