EnumHelper.cs
6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace NCC.Code
{
/// <summary>
/// 枚举帮助类
/// </summary>
public class EnumHelper
{
/// <summary>
/// 返回枚举值的描述信息。
/// </summary>
/// <param name="value">要获取描述信息的枚举值。</param>
/// <returns>枚举值的描述信息。</returns>
public static string GetEnumDesc<T>(int value)
{
try
{
Type enumType = typeof(T);
DescriptionAttribute attr = null;
// 获取枚举常数名称。
string name = Enum.GetName(enumType, value);
if (name != null)
{
// 获取枚举字段。
FieldInfo fieldInfo = enumType.GetField(name);
if (fieldInfo != null)
{
// 获取描述的属性。
attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
}
}
// 返回结果
if (attr != null && !string.IsNullOrEmpty(attr.Description))
return attr.Description;
else
return string.Empty;
}
catch (Exception)
{
return string.Empty;
}
}
/// <summary>
/// 返回枚举项的描述信息。
/// </summary>
/// <param name="e">要获取描述信息的枚举项。</param>
/// <returns>枚举项的描述信息。</returns>
public static string GetEnumDesc(Enum e)
{
if (e == null)
{
return string.Empty;
}
Type enumType = e.GetType();
DescriptionAttribute attr = null;
// 获取枚举字段。
FieldInfo fieldInfo = enumType.GetField(e.ToString());
if (fieldInfo != null)
{
// 获取描述的属性。
attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
}
// 返回结果
if (attr != null && !string.IsNullOrEmpty(attr.Description))
return attr.Description;
else
return string.Empty;
}
/// <summary>
/// 获取枚举描述列表,并转化为键值对
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="isHasAll">是否包含“全部”</param>
/// <param name="filterItem">过滤项</param>
/// <returns></returns>
public static List<EnumKeyValue> EnumDescToList<T>(bool isHasAll, params string[] filterItem)
{
List<EnumKeyValue> list = new List<EnumKeyValue>();
// 如果包含全部则添加
if (isHasAll)
{
list.Add(new EnumKeyValue() { Key = 0, Name = "全部" });
}
#region 方式一
//foreach (var item in typeof(T).GetFields())
//{
// // 获取描述
// var attr = item.GetCustomAttribute(typeof(DescriptionAttribute), true) as DescriptionAttribute;
// if (attr != null && !string.IsNullOrEmpty(attr.Description))
// {
// // 跳过过滤项
// if (Array.IndexOf<string>(filterItem, attr.Description) != -1)
// {
// continue;
// }
// // 添加
// EnumKeyValue model = new EnumKeyValue();
// model.Key = (int)Enum.Parse(typeof(T), item.Name);
// model.Name = attr.Description;
// list.Add(model);
// }
//}
#endregion
#region 方式二
//foreach (int item in Enum.GetValues(typeof(T)))
//{
// // 获取描述
// FieldInfo fi = typeof(T).GetField(Enum.GetName(typeof(T), item));
// var attr = fi.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
// if (attr != null && !string.IsNullOrEmpty(attr.Description))
// {
// // 跳过过滤项
// if (Array.IndexOf<string>(filterItem, attr.Description) != -1)
// {
// continue;
// }
// // 添加
// EnumKeyValue model = new EnumKeyValue();
// model.Key = item;
// model.Name = attr.Description;
// list.Add(model);
// }
//}
#endregion
return list;
}
/// <summary>
/// 获取枚举值列表,并转化为键值对
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="isHasAll">是否包含“全部”</param>
/// <param name="filterItem">过滤项</param>
/// <returns></returns>
public static List<EnumKeyValue> EnumToList<T>(bool isHasAll, params string[] filterItem)
{
List<EnumKeyValue> list = new List<EnumKeyValue>();
// 如果包含全部则添加
if (isHasAll)
{
list.Add(new EnumKeyValue() { Key = -1, Name = "全部" });
}
foreach (int item in Enum.GetValues(typeof(T)))
{
string name = Enum.GetName(typeof(T), item);
// 跳过过滤项
if (Array.IndexOf<string>(filterItem, name) != -1)
{
continue;
}
// 添加
EnumKeyValue model = new EnumKeyValue();
model.Key = item;
model.Name = name;
model.Desc = EnumHelper.GetEnumDesc<T>(item);
list.Add(model);
}
return list;
}
}
/// <summary>
/// 枚举键值对
/// </summary>
public class EnumKeyValue
{
public int Key { get; set; }
public string Desc { get; set; }
public string Name { get; set; }
}
}