-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnumHelper.cs
221 lines (185 loc) · 6.72 KB
/
EnumHelper.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace SuperFramework
{
/// <summary>
/// 说明:枚举帮助类
/// 作者:不良帥
/// 日期:2016-07-27
/// </summary>
public static class EnumHelper
{
#region Method
/// <summary>
/// 返回指定枚举类型的指定值的描述
/// </summary>
/// <param name="t">枚举类型</param>
/// <param name="v">枚举值</param>
/// <returns>返回值</returns>
public static string GetDescription(Type t, object v)
{
try
{
FieldInfo fi = t.GetField(GetName(t, v));
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : GetName(t, v);
}
catch
{
return "UNKNOWN";
}
}
private static string GetName(Type t, object v)
{
try
{
return Enum.GetName(t, v);
}
catch
{
return "UNKNOWN";
}
}
/// <summary>
/// 返回指定枚举类型的所有枚举项
/// </summary>
/// <param name="t">枚举类型</param>
/// <returns>返回值</returns>
public static SortedList GetStatus(Type t)
{
var list = new SortedList();
Array a = Enum.GetValues(t);
for (int i = 0; i < a.Length; i++)
{
string enumName = a.GetValue(i).ToString();
var enumKey = (int)Enum.Parse(t, enumName);
string enumDescription = GetDescription(t, enumKey);
list.Add(enumKey, enumDescription);
}
return list;
}
#endregion
#region 通过字符串获取枚举成员实例
/// <summary>
/// 通过字符串获取枚举成员实例
/// </summary>
/// <typeparam name="T">枚举名,比如Enum1</typeparam>
/// <param name="member">枚举成员的常量名或常量值,
/// 范例:Enum1枚举有两个成员A=0,B=1,则传入"A"或"0"获取 Enum1.A 枚举类型</param>
/// <returns>返回值</returns>
public static T GetInstance<T>(string member)
{
return (T)Enum.Parse(typeof(T), member, true);
}
#endregion
#region 获取枚举成员名称和成员值的键值对集合
/// <summary>
/// 获取枚举成员名称和成员值的键值对集合
/// </summary>
/// <typeparam name="T">枚举名,比如Enum1</typeparam>
/// <returns>返回值</returns>
public static Dictionary<string, object> GetMemberKeyValue<T>()
{
//获取枚举所有成员名称
string[] memberNames = GetMemberNames<T>();
//遍历枚举成员
//返回哈希表
return memberNames.ToDictionary(memberName => memberName, GetMemberValue<T>);
}
#endregion
#region 获取枚举所有成员名称
/// <summary>
/// 获取枚举所有成员名称
/// </summary>
/// <typeparam name="T">枚举名,比如Enum1</typeparam>
/// <returns>返回值</returns>
public static string[] GetMemberNames<T>()
{
return Enum.GetNames(typeof(T));
}
#endregion
#region 获取枚举成员的名称
/// <summary>
/// 获取枚举成员的名称
/// </summary>
/// <typeparam name="T">枚举名,比如Enum1</typeparam>
/// <param name="member">枚举成员实例或成员值,
/// 范例:Enum1枚举有两个成员A=0,B=1,则传入Enum1.A或0,获取成员名称"A"</param>
/// <returns>返回值</returns>
public static string GetMemberName<T>(object member)
{
//转成基础类型的成员值
Type underlyingType = GetUnderlyingType(typeof(T));
// object memberValue = MyConvertHelper.ConvertTo(member, underlyingType);
//获取枚举成员的名称
return Enum.GetName(typeof(T), member);
}
#endregion
#region 获取枚举所有成员值
/// <summary>
/// 获取枚举所有成员值
/// </summary>
/// <typeparam name="T">枚举名,比如Enum1</typeparam>
/// <returns>返回值</returns>
public static Array GetMemberValues<T>()
{
return Enum.GetValues(typeof(T));
}
#endregion
#region 获取枚举成员的值
/// <summary>
/// 获取枚举成员的值
/// </summary>
/// <typeparam name="T">枚举名,比如Enum1</typeparam>
/// <param name="memberName">枚举成员的常量名,
/// 范例:Enum1枚举有两个成员A=0,B=1,则传入"A"获取0</param>
/// <returns>返回值</returns>
public static object GetMemberValue<T>(string memberName)
{
//获取基础类型
Type underlyingType = GetUnderlyingType(typeof(T));
//获取枚举实例
// var instance = GetInstance<T>(memberName);
//获取枚举成员的值
// return MyConvertHelper.ConvertTo(instance, underlyingType);
// bool TryParse = Enum.IsDefined(underlyingType, memberName);
//T t=default(T);
//if (IsDefined<T>( memberName))
//{
// object m = Enum.Parse(underlyingType, memberName);
// t= (T)m;
//}
int x = (int)Enum.Parse(underlyingType, memberName);
return x;
}
#endregion
#region 获取枚举的基础类型
/// <summary>
/// 获取枚举的基础类型
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <returns>返回值</returns>
public static Type GetUnderlyingType(Type enumType)
{
//获取基础类型
return Enum.GetUnderlyingType(enumType);
}
#endregion
#region 检测枚举是否包含指定成员
/// <summary>
/// 检测枚举是否包含指定成员
/// </summary>
/// <typeparam name="T">枚举名,比如Enum1</typeparam>
/// <param name="member">枚举成员名或成员值</param>
/// <returns>返回值</returns>
public static bool IsDefined<T>(string member)
{
return Enum.IsDefined(typeof(T), member);
}
#endregion
}
}