schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using SystemEnum = System.Enum;
 
namespace HxEnum
{
    public static class EnumManagement
    {
        #region 获取枚举描述
        /// <summary>
        /// 获取枚举描述
        /// </summary>
        /// <param name="enumSubitem">枚举值</param>
        /// <returns>返回枚举描述</returns>
        public static string GetEnumDescription(object enumSubitem)
        {
            enumSubitem = (System.Enum)enumSubitem;
            string strValue = enumSubitem.ToString();
 
            FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);
 
            if (fieldinfo != null)
            {
 
                Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
 
                if (objs == null || objs.Length == 0)
                {
                    return strValue;
                }
                else
                {
                    DescriptionAttribute da = (DescriptionAttribute)objs[0];
                    return da.Description;
                }
            }
            else
            {
                return "不限";
            }
 
        }
        #endregion
 
        #region 根据枚举的int值获取枚举值的详细说明
        /// <summary>
        /// 根据枚举的int值获取枚举值的详细说明
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="currentValue">枚举的Int值</param>
        /// <returns>枚举值说明</returns>
        public static string GetFieldTextByValue<T>(int currentValue)
        {
            T currentType = GetField<T>(currentValue);
            //return GetFieldText<T>(currentType);
            return GetEnumDescription(currentType);
        }
 
        /// <summary>
        /// 根据枚举的int值类型转换为枚举类型
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="currentValue">枚举的int值</param>
        /// <returns>枚举类型</returns>
        public static T GetField<T>(int currentValue)
        {
            return (T)SystemEnum.Parse(typeof(T), currentValue.ToString());
        }
 
        /// <summary>
        /// 获取枚举值的说明
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="enumType">枚举类型值</param>
        /// <returns>枚举值说明</returns>
        public static string GetFieldText<T>(T enumType)
        {
            Type currentType = enumType.GetType();
            //获取字段信息   
            FieldInfo[] fields = currentType.GetFields();
 
            if (fields == null || fields.Length < 1)
            {
 
                return enumType.ToString();
            }
 
            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo field = fields[i];
 
                //判断名称是否相等   
                if (field.Name != enumType.ToString())
                {
                    continue;
                }
 
                //反射自定义属性   
                object[] attributes = field.GetCustomAttributes(
                    typeof(DescriptionAttribute), false);
 
                foreach (Attribute attribute in attributes)
                {
                    //类型转换找到一个Description,用Description作为成员名称   
                    DescriptionAttribute currentAttribute =
                        attribute as DescriptionAttribute;
 
                    if (currentAttribute != null)
                    {
                        return currentAttribute.Description;
                    }
                }
            }
 
            //如果没有检测到合适的注释,则用默认名称   
            return enumType.ToString();
 
        }
        #endregion
 
        #region 获取枚举值int
        /// <summary>
        /// 获取枚举值int
        /// </summary>
        /// <param name="enumSubitem">枚举值</param>
        /// <returns>返回枚举值</returns>
        public static int GetEnumValue(object enumSubitem)
        {
            return (int)enumSubitem;
        }
        #endregion
 
        #region 获取枚举所有值,变成字典
        /// <summary>
        /// 获取枚举所有值,变成字典(键:字母,值:中文)
        /// </summary>
        /// <typeparam name="T">枚举对象</typeparam>
        /// <returns></returns>
        public static Dictionary<string, string> EnumToDictionaryKey<T>()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (!typeof(T).IsEnum)
            {
                return dic;
            }
            string desc = string.Empty;
            foreach (var item in Enum.GetValues(typeof(T)))
            {
                var attrs = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (attrs != null && attrs.Length > 0)
                {
                    DescriptionAttribute descAttr = attrs[0] as DescriptionAttribute;
                    desc = descAttr.Description;
                }
                dic.Add(item.ToString(), desc);
            }
            return dic;
        }
 
        /// <summary>
        /// 获取枚举所有值,变成字典(键:数字,值:中文)
        /// </summary>
        /// <typeparam name="T">枚举对象</typeparam>
        /// <returns></returns>
        public static Dictionary<string, string> EnumToDictionaryValue<T>()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (!typeof(T).IsEnum)
            {
                return dic;
            }
            string desc = string.Empty;
            foreach (var item in Enum.GetValues(typeof(T)))
            {
                var attrs = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (attrs != null && attrs.Length > 0)
                {
                    DescriptionAttribute descAttr = attrs[0] as DescriptionAttribute;
                    desc = descAttr.Description;
                }
                dic.Add(GetEnumValue(item).ToString(), desc);
            }
            return dic;
        }
 
        /// <summary>
        /// 字符串转Enum
        /// </summary>
        /// <typeparam name="T">枚举</typeparam>
        /// <param name="str">字符串</param>
        /// <returns>转换的枚举</returns>
        public static T ToEnum<T>(this string str)
        {
            return (T)Enum.Parse(typeof(T), str);
        }
 
        /// <summary>
        /// 获取所有枚举
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<object> GetALLEnum<T>()
        {
            List<object> list = new List<object>();
 
            foreach (var item in Enum.GetValues(typeof(T)))
            {
                list.Add(item);
            }
            return list;
        }
        #endregion 
    }
}