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 |
using
System; using
System.Reflection; using
System.Collections; using
System.Collections.Generic; namespace
AttributeMeta { [AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum)] public
class EnumDescription : Attribute { private
FieldInfo fieldInfo; public
EnumDescription( string
text, int
rank) { this .Text = text; this .Rank = rank; } /// <summary> /// 描述枚举值,默认排序为5 /// </summary> /// <param name="enumDisplayText">描述内容</param> public
EnumDescription( string
text) : this (text, 5) { } public
string Text { get ; set ; } public
int Rank { get ; set ; } public
int Value { get { return
( int )fieldInfo.GetValue( null ); } } public
string FieldName { get { return
fieldInfo.Name; } } #region 对枚举描述属性的解释相关函数 /// <summary> /// 排序类型 /// </summary> public
enum SortType { /// <summary> ///按枚举顺序默认排序 /// </summary> Default, /// <summary> /// 按描述值排序 /// </summary> DisplayText, /// <summary> /// 按排序熵 /// </summary> Rank } private
static Hashtable cachedEnum = new
Hashtable(); /// <summary> /// 得到对枚举的描述文本 /// </summary> /// <param name="enumType">枚举类型</param> /// <returns></returns> public
static string GetEnumText(Type enumType) { EnumDescription[] eds = (EnumDescription[])enumType.GetCustomAttributes( typeof (EnumDescription), false ); if
(eds.Length != 1) return
string .Empty; return
eds[0].Text; } /// <summary> /// 获得指定枚举类型中,指定值的描述文本。 /// </summary> /// <param name="enumValue">枚举值,不要作任何类型转换</param> /// <returns>描述字符串</returns> public
static string GetFieldText( object
enumValue) { EnumDescription[] descriptions = GetFieldTexts(enumValue.GetType(), SortType.Default); foreach
(EnumDescription ed in
descriptions) { if
(ed.fieldInfo.Name == enumValue.ToString()) return
ed.Text; } return
string .Empty; } /// <summary> /// 得到枚举类型定义的所有文本,按定义的顺序返回 /// </summary> /// <exception cref="NotSupportedException"></exception> /// <param name="enumType">枚举类型</param> /// <returns>所有定义的文本</returns> public
static EnumDescription[] GetFieldTexts(Type enumType) { return
GetFieldTexts(enumType, SortType.Default); } /// <summary> /// 得到枚举类型定义的所有文本 /// </summary> /// <exception cref="NotSupportedException"></exception> /// <param name="enumType">枚举类型</param> /// <param name="sortType">指定排序类型</param> /// <returns>所有定义的文本</returns> public
static EnumDescription[] GetFieldTexts(Type enumType, SortType sortType) { EnumDescription[] descriptions = null ; //缓存中没有找到,通过反射获得字段的描述信息 if
(cachedEnum.Contains(enumType.FullName) == false ) { FieldInfo[] fields = enumType.GetFields(); ArrayList edAL = new
ArrayList(); foreach
(FieldInfo fi in
fields) { object [] eds = fi.GetCustomAttributes( typeof (EnumDescription), false ); if
(eds.Length != 1) continue ; ((EnumDescription)eds[0]).fieldInfo = fi; edAL.Add(eds[0]); } cachedEnum.Add(enumType.FullName, (EnumDescription[])edAL.ToArray( typeof (EnumDescription))); } descriptions = (EnumDescription[])cachedEnum[enumType.FullName]; if
(descriptions.Length <= 0) throw
new NotSupportedException( "枚举类型["
+ enumType.Name + "]未定义属性EnumValueDescription" ); //按指定的属性冒泡排序 for
( int
m = 0; m < descriptions.Length; m++) { //默认就不排序了 if
(sortType == SortType.Default) break ; for
( int
n = m; n < descriptions.Length; n++) { EnumDescription temp; bool
swap = false ; switch
(sortType) { case
SortType.Default: break ; case
SortType.DisplayText: if
( string .Compare(descriptions[m].Text, descriptions[n].Text) > 0) swap = true ; break ; case
SortType.Rank: if
(descriptions[m].Rank > descriptions[n].Rank) swap = true ; break ; } if
(swap) { temp = descriptions[m]; descriptions[m] = descriptions[n]; descriptions[n] = temp; } } } return
descriptions; } /// <summary> /// 获得枚举类型数据的列表 /// </summary> /// <param name="enumType">枚举类型</param> /// <returns>Meta 列表</returns> public
static List<Meta> GetMeta(Type enumType) { List<Meta> list = new
List<Meta>(); FieldInfo[] fields = enumType.GetFields(); foreach
(FieldInfo fi in
fields) { object [] eds = fi.GetCustomAttributes( typeof (EnumDescription), false ); if
(eds.Length != 1) continue ; ((EnumDescription)eds[0]).fieldInfo = fi; var
item = eds[0] as
EnumDescription; Meta meta = new
Meta { MetaName = item.FieldName, MetaRank = item.Rank, MetaText = item.Text, MetaValue = item.Value }; list.Add(meta); } return
list; } /// <summary> /// 返回枚举类型数据的哈希表 /// </summary> /// <param name="enumType">枚举类型</param> /// <returns>Hashtable</returns> public
static Hashtable GetMetaTable(Type enumType) { Hashtable table = new
Hashtable(); FieldInfo[] fields = enumType.GetFields(); foreach
(FieldInfo fi in
fields) { object [] eds = fi.GetCustomAttributes( typeof (EnumDescription), false ); if
(eds.Length != 1) continue ; ((EnumDescription)eds[0]).fieldInfo = fi; var
item = eds[0] as
EnumDescription; table.Add(item.Value, item.Text); } return
table; } /// <summary> /// 根据枚举值获得枚举文本 /// </summary> /// <param name="enumType">枚举类型</param> /// <param name="key">枚举值</param> /// <returns>Text</returns> public
static object GetMetaValue(Type enumType, int
key) { object
value = null ; Hashtable table = GetMetaTable(enumType); if
(table.Count > 0) { value = table[key]; } return
value; } /// <summary> /// 根据枚举文本获得枚举值 /// </summary> /// <param name="enumType">枚举类型</param> /// <param name="value">枚举文本</param> /// <returns>Value</returns> public
static object GetMetaKey(Type enumType, string
value) { object
key = null ; Hashtable table = GetMetaTable(enumType); if
(table.Count > 0) { foreach
(DictionaryEntry de in
table) { if
(de.Value.Equals(value)) { key = de.Key; } } } return
key; } #endregion } public
class Meta { public
string MetaName { get ; set ; } public
int MetaValue { get ; set ; } public
string MetaText { get ; set ; } public
int MetaRank { get ; set ; } } } |
EnumDescription,布布扣,bubuko.com
原文:http://www.cnblogs.com/wlwjc/p/3578541.html