1 public class EnumHelper
2 {
3 #region 获取枚举
4 public static List<EnumValue> GetEnumList(Type enumType)
5 {
6 var list = new List<EnumValue>();
7 string[] strArray = Enum.GetNames(enumType);
8 foreach (string item in strArray)
9 {
10 int enumValue = (int)Enum.Parse(enumType, item, true);
11 string text = GetEnumDescription(enumType, item);
12
13 list.Add(new EnumValue { Text = text, Value = enumValue });
14
15 }
16 return list;
17 }
18
19 public static List<EnumberEntity> EnumToList<T>()
20 {
21 var list = new List<EnumberEntity>();
22
23 foreach (var e in Enum.GetValues(typeof(T)))
24 {
25 var m = new EnumberEntity();
26 var objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
27 if (objArr.Length > 0)
28 {
29 var da = objArr[0] as DescriptionAttribute;
30 if (da != null) m.Description = da.Description;
31 }
32 m.Value = Convert.ToInt32(e);
33 m.Name = e.ToString();
34 list.Add(m);
35 }
36 return list;
37 }
38
39 /// <summary>
40 /// 获得描述
41 /// </summary>
42 /// <param name="value">枚举</param>
43 /// <returns>描述内容</returns>
44 public static string GetEnumDescription(Enum value)
45 {
46 if (value == null)
47 {
48 throw new ArgumentNullException("value");
49 }
50
51 string description = value.ToString();
52
53 Type eunmtype = value.GetType();
54
55 return GetEnumDescription(eunmtype, description);
56 }
57
58 public static string GetEnumDescription(Type enumType, string name)
59 {
60 try
61 {
62 DescriptionAttribute[] attributes = (DescriptionAttribute[])enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
63 if (attributes != null && attributes.Length > 0)
64 {
65 name = attributes[0].Description;
66 }
67 else
68 {
69 name = name.ToString();
70 }
71 return name;
72 }
73 catch
74 {
75 return string.Empty;
76 }
77 }
78 #endregion
79 }
80
81 public class EnumValue
82 {
83 public int Value { get; set; }
84 public string Text { get; set; }
85 }
86
87 public class EnumberEntity
88 {
89 /// <summary>
90 /// 枚举的描述
91 /// </summary>
92 public string Description { set; get; }
93
94 /// <summary>
95 /// 枚举名称
96 /// </summary>
97 public string Name { set; get; }
98
99 /// <summary>
100 /// 枚举对象的值
101 /// </summary>
102 public int Value { set; get; }
103 }