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
using System.ComponentModel;
using System.Reflection;
 
namespace WIDESEA_Core.Enums
{
    public static class EnumHelper
    {
        /// <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>
        /// 获取枚举项描述信息 例如GetEnumDesc(Days.Sunday)
        /// </summary>
        /// <param name="en">枚举项 如Days.Sunday</param>
        /// <returns></returns>
        public static string GetIntegralRuleTypeEnumDesc(this Enum en)
        {
            Type type = en.GetType();
            MemberInfo[] memInfo = type.GetMember(en.ToString());
            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if (attrs != null && attrs.Length > 0)
                    return ((DescriptionAttribute)attrs[0]).Description;
            }
            return en.ToString();
        }
        public static string GetDescriptionFromEnums(int? value, params Type[] enumTypes)
        {
            if (!value.HasValue) return string.Empty;
 
            foreach (var enumType in enumTypes)
            {
                if (Enum.IsDefined(enumType, value.Value))
                {
                    var enumValue = Enum.ToObject(enumType, value.Value);
                    return GetDescription(enumValue);
                }
            }
 
            return string.Empty;
        }
 
        /// <summary>
        /// 获取枚举描述
        /// </summary>
        public static string GetDescription(object value)
        {
            if (value == null) return string.Empty;
 
            var field = value.GetType().GetField(value.ToString());
            if (field == null) return value.ToString();
 
            var descriptionAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false)
                                          .FirstOrDefault() as DescriptionAttribute;
 
            return descriptionAttribute?.Description ?? value.ToString();
        }
        /// <summary>
        /// 获取枚举集合
        /// </summary>
        /// <typeparam name="T">枚举类名称</typeparam>
        /// <returns></returns>
        public static IEnumerable<EnumModel> GetEnumList<T>()
        {
            var model = default(T);
            FieldInfo[] fieldinfo = typeof(T).GetFields();
            List<EnumModel> result = new List<EnumModel>();
            foreach (FieldInfo field in fieldinfo)
            {
                EnumModel enumModel = new EnumModel();
                if (!(Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute))
                {
                    enumModel.Desc = field.GetValue(model).ToString();
                }
                else
                {
                    enumModel.Desc = attribute.Description;
                }
                enumModel.Value = field.GetValue(model).GetHashCode();
                enumModel.Key = field.GetValue(model) as ValueType;
                if (field.GetValue(model).ToString() != "0")
                {
                    result.Add(enumModel);
                }
 
            }
            return result;
        }
    }
 
    public class EnumModel
    {
        /// <summary>
        /// Enum的值
        /// </summary>
        public int Value { get; set; }
        /// <summary>
        /// Enum的key
        /// </summary>
        public ValueType Key { get; set; }
        /// <summary>
        /// Enum描述
        /// </summary>
        public string Desc { get; set; }
    }
 
}