EnumHelper.cs
10 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System.ComponentModel;
namespace HHECS.DAQShared.Common.Utils
{
/// <summary>
/// 枚举帮助类
/// </summary>
public static class EnumHelper
{
/// <summary>
/// 获取属性描述
/// </summary>
/// <param name="temp"></param>
/// <returns></returns>
public static string GetDescription<T>(string temp)
{
string Description = "";
Dictionary<string, object> dicEnum = EnumListDic<T>();
foreach (var val in dicEnum.Where(t => t.Value.ToString() == temp).ToArray())
{
return val.Value.ToString() == temp ? (Description += val.Key) : (Description += temp);
}
return Description;
}
#region 枚举转字典
/// <summary>
/// 枚举转字典集合
/// </summary>
/// <returns>返回生成的字典集合</returns>
public static Dictionary<string, object> EnumListDic<T>()
{
Dictionary<string, object> dicEnum = new Dictionary<string, object>();
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
return dicEnum;
}
string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组
foreach (var item in fieldstrs)
{
string description = string.Empty;
var field = enumType.GetField(item);
object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
if (arr != null && arr.Length > 0)
{
description = ((DescriptionAttribute)arr[0]).Description; //属性描述
}
else
{
description = item; //描述不存在取字段名称
}
dicEnum.Add(description, (int)Enum.Parse(enumType, item)); //不用枚举的value值作为字典key值的原因从枚举例子能看出来,其实这边应该判断他的值不存在,默认取字段名称
}
return dicEnum;
}
/// <summary>
/// 枚举转字典集合
/// </summary>
/// <returns>返回生成的字典集合</returns>
public static Dictionary<string, object> EnumListDicString<T>()
{
Dictionary<string, object> dicEnum = new Dictionary<string, object>();
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
return dicEnum;
}
string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组
foreach (var item in fieldstrs)
{
string description = string.Empty;
var field = enumType.GetField(item);
object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
if (arr != null && arr.Length > 0)
{
description = ((DescriptionAttribute)arr[0]).Description; //属性描述
}
else
{
description = item; //描述不存在取字段名称
}
dicEnum.Add(description, Enum.Parse(enumType, item).ToString()); //不用枚举的value值作为字典key值的原因从枚举例子能看出来,其实这边应该判断他的值不存在,默认取字段名称
}
return dicEnum;
}
/// <summary>
/// 枚举转字典集合
/// </summary>
/// <typeparam name="T">枚举类名称</typeparam>
/// <param name="keyDefault">默认key值</param>
/// <param name="valueDefault">默认value值</param>
/// <returns>返回生成的字典集合</returns>
public static Dictionary<string, object> EnumListDic<T>(string keyDefault, string valueDefault = "")
{
Dictionary<string, object> dicEnum = new Dictionary<string, object>();
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
return dicEnum;
}
if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项
{
dicEnum.Add(keyDefault, valueDefault);
}
string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组
foreach (var item in fieldstrs)
{
string description = string.Empty;
var field = enumType.GetField(item);
object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
if (arr != null && arr.Length > 0)
{
description = ((DescriptionAttribute)arr[0]).Description; //属性描述
}
else
{
description = item; //描述不存在取字段名称
}
dicEnum.Add(description, (int)Enum.Parse(enumType, item)); //不用枚举的value值作为字典key值的原因从枚举例子能看出来,其实这边应该判断他的值不存在,默认取字段名称
}
return dicEnum;
}
/// <summary>
/// 枚举转字典集合
/// </summary>
/// <returns>返回生成的字典集合</returns>
public static Dictionary<string, object> EnumListDicString<T>(string keyDefault, string valueDefault = "")
{
Dictionary<string, object> dicEnum = new Dictionary<string, object>();
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
return dicEnum;
}
if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项
{
dicEnum.Add(keyDefault, valueDefault);
}
string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组
foreach (var item in fieldstrs)
{
string description = string.Empty;
var field = enumType.GetField(item);
object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
if (arr != null && arr.Length > 0)
{
description = ((DescriptionAttribute)arr[0]).Description; //属性描述
}
else
{
description = item; //描述不存在取字段名称
}
dicEnum.Add(description, Enum.Parse(enumType, item).ToString()); //不用枚举的value值作为字典key值的原因从枚举例子能看出来,其实这边应该判断他的值不存在,默认取字段名称
}
return dicEnum;
}
#endregion
#region 枚举转list
/// <summary>
/// 将枚举转为List INT的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<int> EnumListInt<T>()
{
List<int> list = new List<int>();
foreach (var e in Enum.GetNames(typeof(T)))
{
int v = (int)Enum.Parse(typeof(T), e.ToString());
list.Add(v);
}
return list;
}
/// <summary>
/// 将枚举转为List string的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<string> EnumListString<T>()
{
List<string> list = new List<string>();
foreach (var e in Enum.GetValues(typeof(T)))
{
string v = ((int)Enum.Parse(typeof(T), e.ToString())).ToString();
list.Add(v);
}
return list;
}
#endregion
#region Enum
/// <summary>
/// 获取枚举成员的值(this是扩展方法的标志)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static int GetIndexInt(this Enum obj)
{
return Convert.ToInt32(obj);
}
/// <summary>
/// 获取枚举成员的值(this是扩展方法的标志)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string GetIndexString(this Enum obj)
{
return Convert.ToInt32(obj).ToString();
}
public static T ToEnum<T>(this string obj) where T : struct
{
if (string.IsNullOrEmpty(obj))
{
return default(T);
}
try
{
return (T)Enum.Parse(typeof(T), obj, true);
}
catch (Exception)
{
return default(T);
}
}
/// <summary>
/// 获取指定枚举成员的描述
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ToDescriptionOrString(this Enum obj)
{
var attribs = (DescriptionAttribute[])obj.GetType().GetField(obj.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
return attribs.Length > 0 ? attribs[0].Description : obj.ToString();
}
/// <summary>
/// 根据枚举值,获取指定枚举类的成员描述
/// </summary>
/// <param name="type"></param>
/// <param name="id"></param>
/// <returns></returns>
public static string GetDescriptionString(this Type type, int? id)
{
var values = from Enum e in Enum.GetValues(type)
select new { id = e.GetIndexInt(), name = e.ToDescriptionOrString() };
if (!id.HasValue) id = 0;
try
{
return values.ToList().Find(c => c.id == id).name;
}
catch
{
return "";
}
}
#endregion
}
}