WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
 
namespace JiepeiWMS.Common.Helper
{
    /// <summary>
    /// 枚举帮助类
    /// 
    /// @2015.6.18
    /// </summary>
    public static class EnumHelper
    {
        /// <summary>
        /// 转成列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<T> ToList<T>()
        {
            var fields = typeof(T).GetFields();
            var list = (from f in fields
                        where f.FieldType == typeof(T)
                        select (T)f.GetRawConstantValue()
                ).ToList();
            return list;
        }
 
        /// <summary>
        /// 返回枚举值对应的NoteAttribute注释信息        
        /// </summary>
        /// <typeparam name="T">枚举</typeparam>
        /// <param name="val">值</param>
        /// <returns>注释</returns>
        public static string GetNoteByVal<T>(object val) where T : struct
        {
            var field = typeof(T).GetFields().FirstOrDefault(x => x.FieldType == typeof(T) && x.GetRawConstantValue() == val);
            if (field == null) return string.Empty;
            var attr = field.GetCustomAttributes().FirstOrDefault(x => x.GetType() == typeof(NoteAttribute)) as NoteAttribute;
            return attr == null ? string.Empty : attr.Note;
        }
 
        /// <summary>
        /// 返回常量对应的NoteAttribute注释信息        
        /// </summary>
        /// <typeparam name="T">常量类</typeparam>
        /// <param name="val">值</param>
        /// <returns>注释</returns>
        public static string GetNoteByConst<T>(string val)
        {
            var field = typeof(T).GetFields().FirstOrDefault(x => x.Name == val);
            if (field == null) return string.Empty;
            var attr = field.GetCustomAttributes().FirstOrDefault(x => x.GetType() == typeof(NoteAttribute)) as NoteAttribute;
            return attr == null ? string.Empty : attr.Note;
        }
 
        /// <summary>
        /// 枚举值转int16
        /// </summary>
        /// <param name="source">枚举</param>
        /// <returns>int16(short)</returns>
        public static short ToShort(this System.Enum source)
        {
            return Convert.ToInt16(source.GetHashCode());
        }
 
        /// <summary>
        /// 枚举值转byte
        /// </summary>
        /// <param name="source">枚举</param>
        /// <returns>byte</returns>
        public static byte ToByte(this System.Enum source)
        {
            return Convert.ToByte(source.GetHashCode());
        }
 
        /// <summary>
        /// 返回枚举注释
        /// </summary>
        /// <param name="source">枚举</param>
        /// <returns>注释</returns>
        public static string ToNote(this System.Enum source)
        {
            var field = source.GetType().GetField(source.ToString());
            if (field == null) { return string.Empty; }
            var attr = field.GetCustomAttributes().FirstOrDefault(x => x.GetType() == typeof(NoteAttribute)) as NoteAttribute;
            return attr == null ? string.Empty : attr.Note;
        }
 
        public static Dictionary<int, string> GetNotes<TEnum>() where TEnum : struct
        {
            var notes = new Dictionary<int, string>();
 
            var fields = typeof(TEnum).GetFields();
            foreach (var f in fields)
            {
                if (f.FieldType == typeof(TEnum))
                {
                    var attr = f.GetCustomAttribute(typeof(NoteAttribute)) as NoteAttribute;
                    string note = null;
                    if (attr != null)
                    {
                        note = attr.Note;
                    }
 
                    var rawConstantValue = (int)f.GetRawConstantValue();
                    notes.Add(rawConstantValue, note);
                }
 
            }
 
            return notes;
        }
 
        public static string ToNote(this int val, Type T)
        {
            Type type = T;
            string name = System.Enum.GetName(type, val);
            if (name == null)
                return string.Empty;
            FieldInfo fd = type.GetField(name);
            object[] attrs = fd.GetCustomAttributes(typeof(NoteAttribute), false);
            string note = string.Empty;
            foreach (NoteAttribute attr in attrs)
            {
                note = attr.Note;
            }
            return note;
        }
 
        public static string ToNote(this int? val, Type T)
        {
            if (val == null) return "";
            return ToNote(val.Value, T);
        }
 
        /// <summary>
        /// 获取常量的属性
        /// </summary>
        /// <param name="val"></param>
        /// <param name="T"></param>
        /// <returns></returns>
        public static string ToConstNote(this string val, Type T)
        {
            Type type = T;
            var info = type.GetFields();
            if (val == null)
                return string.Empty;
            FieldInfo fd = type.GetField(val);
            object[] attrs = fd.GetCustomAttributes(typeof(NoteAttribute), false);
            string note = string.Empty;
            foreach (NoteAttribute attr in attrs)
            {
                note = attr.Note;
            }
            return note;
        }
 
        #region 戴雁冰扩展
        /// <summary>
        /// 获取类型的说明
        /// </summary>
        public static string _GetNoteAttr(this Type TypeObj)
        {
            var enumAttr = TypeObj.GetCustomAttributes(_NoteAttribute, true);
            if (enumAttr.Length == 0) { return string.Empty; }
 
            var desc = enumAttr[0] as NoteAttribute;
            return desc.Note ?? string.Empty;
        }
        /// <summary>
        /// 转成值与说明的字典
        /// </summary>
        public static Dictionary<TNumType, string> _GetValueNoteDicFromEnumType<TNumType>(this Type EnmType)
        {
            var ns = System.Enum.GetNames(EnmType);
            var vs = System.Enum.GetValues(EnmType); ;
            var dic = new Dictionary<TNumType, string>(ns.Length);
            for (int i = 0; i < ns.Length; i++)
            {
                FieldInfo finfo = EnmType.GetField(ns[i]);
                object[] enumAttr = finfo.GetCustomAttributes(_NoteAttribute, true);
                string remark = string.Empty;
                if (enumAttr.Length > 0)
                {
                    var desc = enumAttr[0] as NoteAttribute;
                    if (!string.IsNullOrEmpty(desc.Note))
                    {
                        remark = desc.Note;
                    }
                }
                dic[(TNumType)vs.GetValue(i)] = remark;
            }
            return dic;
        }
        static Type _NoteAttribute = typeof(NoteAttribute);
        /// <summary>
        /// 获取类型的说明
        /// </summary>
        public static string _GetDescriptionAttr(this Type TypeObj)
        {
            var enumAttr = TypeObj.GetCustomAttributes(_DesciptionAttribute, true);
            if (enumAttr.Length == 0) { return string.Empty; }
 
            var desc = enumAttr[0] as DescriptionAttribute;
            return desc.Description ?? string.Empty;
        }
        /// <summary>
        /// 获取枚举值的说明
        /// </summary>
        public static string _GetDescriptionByEnumValue<TEnum>(this TEnum EnmValue)
        {
            var EnmType = typeof(TEnum);
 
            FieldInfo finfo = EnmType.GetField(EnmValue.ToString());
            if (finfo == null) { return string.Empty; }
 
            var enumAttr = finfo.GetCustomAttributes(_DesciptionAttribute, true);
            if (enumAttr.Length == 0) { return string.Empty; }
 
            var desc = enumAttr[0] as DescriptionAttribute;
            return desc.Description ?? string.Empty;
        }
        /// <summary>
        /// 获取枚举值的说明
        /// </summary>
        public static string _GetNoteByEnumValue<TEnum>(this TEnum EnmValue)
        {
            var EnmType = typeof(TEnum);
 
            FieldInfo finfo = EnmType.GetField(EnmValue.ToString());
            if (finfo == null) { return string.Empty; }
 
            var enumAttr = finfo.GetCustomAttributes(_NoteAttribute, true);
            if (enumAttr.Length == 0) { return string.Empty; }
 
            var desc = enumAttr[0] as NoteAttribute;
            return desc.Note ?? string.Empty;
        }
        /// <summary>
        /// 转成值与说明的字典
        /// </summary>
        public static Dictionary<TNumType, string> _GetValueDescriptionDicFromEnumType<TNumType>(this Type EnmType)
        {
            var ns = System.Enum.GetNames(EnmType);
            var vs = System.Enum.GetValues(EnmType); ;
            var dic = new Dictionary<TNumType, string>(ns.Length);
            for (int i = 0; i < ns.Length; i++)
            {
                FieldInfo finfo = EnmType.GetField(ns[i]);
                object[] enumAttr = finfo.GetCustomAttributes(_DesciptionAttribute, true);
                string remark = string.Empty;
                if (enumAttr.Length > 0)
                {
                    var desc = enumAttr[0] as DescriptionAttribute;
                    if (!string.IsNullOrEmpty(desc.Description))
                    {
                        remark = desc.Description;
                    }
                }
                dic.Add((TNumType)vs.GetValue(i), remark);
            }
            return dic;
        }
        static Type _DesciptionAttribute = typeof(DescriptionAttribute);
        /// <summary>
        /// 获取枚举
        /// </summary>
        public static TEnum _GetEnumByName<TEnum>(this string Name, TEnum DefValue) where TEnum : struct
        {
            TEnum enm;
            return System.Enum.TryParse<TEnum>(Name, true, out enm) ? enm : DefValue;
        }
        /// <summary>
        /// 转成名称与值的字典
        /// </summary>
        public static Dictionary<string, TNumType> _GetNameValueDicFromEnumType<TNumType>(this Type EnmType)
        {
            var ns = System.Enum.GetNames(EnmType);
            var vs = System.Enum.GetValues(EnmType); ;
            var dic = new Dictionary<string, TNumType>(ns.Length);
            for (int i = 0; i < ns.Length; i++)
            {
                dic.Add(ns[i], (TNumType)vs.GetValue(i));
            }
            return dic;
        }
        #endregion
    }
 
    public class NoteAttribute : Attribute
    {
        public string Note { get; set; }
 
        public NoteAttribute(string note)
        {
            this.Note = note;
        }
    }
}