admin
2024-09-20 8b7b39172ee548ff303dbd73816cb7c1071f7887
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace CommonHelper
{
    /// <summary>
    /// 对应表的字段名称
    /// </summary>
    public class EnumColnameAttribute : Attribute
    {
        public EnumColnameAttribute(string colname)
        {
            ColName = colname;
        }
 
        public string ColName { get; set; }
    }
 
    /// <summary>
    /// 保留几位小数
    /// </summary>
    public class EnumDecimalDigitsAttribute : Attribute
    {
        public EnumDecimalDigitsAttribute(int digits)
        {
            Digits = digits;
        }
 
        public int Digits;
    }
 
    /// <summary>
    /// 枚举类型操作公共类。
    /// </summary>
    public static class EnumHelper
    {
        /// <summary>
        /// 获取枚举所有成员名称。
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        public static string[] GetNames<T>()
        {
            return Enum.GetNames(typeof(T));
        }
 
        /// <summary>
        /// 检测枚举是否包含指定成员。
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="member">成员名或成员值</param>
        public static bool IsDefined(this Enum value)
        {
            Type type = value.GetType();
            return Enum.IsDefined(type, value);
        }
 
        /// <summary>
        /// 返回指定枚举类型的指定值的描述。
        /// </summary>
        /// <param name="t">枚举类型</param>
        /// <param name="v">枚举值</param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            try
            {
                Type type = value.GetType();
                FieldInfo field = type.GetField(value.ToString());
                DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                return (attributes.Length > 0) ? attributes[0].Description : string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }
 
        public static string GetDisplayName(this Enum eum)
        {
            var type = eum.GetType();//先获取这个枚举的类型
            var field = type.GetField(eum.ToString());//通过这个类型获取到值
            var obj = (DisplayAttribute)field.GetCustomAttribute(typeof(DisplayAttribute));//得到特性
            return obj.Name ?? "";
        }
 
        /// <summary>
        /// 返回指定枚举类型的指定值 对应的列名。
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetColName(this Enum t)
        {
            var t_type = t.GetType();
            var fieldName = Enum.GetName(t_type, t);
            var attributes = t_type.GetField(fieldName).GetCustomAttributes(false);
            var enumDisplayAttribute = attributes.FirstOrDefault(p => p.GetType().Equals(typeof(EnumColnameAttribute))) as EnumColnameAttribute;
            return enumDisplayAttribute == null ? fieldName : enumDisplayAttribute.ColName;
        }
 
        /// <summary>
        /// 返回指定枚举类型的指定值 对应的列名。
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static int GetDecimalDigits(this Enum t)
        {
            var t_type = t.GetType();
            var fieldName = Enum.GetName(t_type, t);
            var attributes = t_type.GetField(fieldName).GetCustomAttributes(false);
            var enumDisplayAttribute = attributes.FirstOrDefault(p => p.GetType().Equals(typeof(EnumDecimalDigitsAttribute))) as EnumDecimalDigitsAttribute;
            return enumDisplayAttribute?.Digits ?? 0;
        }
 
        /// <summary>
        /// 将枚举转换成字典集合
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns></returns>
        public static Dictionary<int,string> Enum2Dict<T>()
        {
 
            Dictionary<int, string> resultList = new Dictionary<int, string>();
            Type type = typeof(T);
            var strList = GetNames<T>().ToList();
            foreach (string key in strList)
            {
                string val = Enum.Format(type, Enum.Parse(type, key), "d");
                resultList.Add( int.Parse(val), key);
            }
            return resultList;
        }
 
        /// <summary>
        /// 将枚举转换成字典集合
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns></returns>
        public static Dictionary<int, string> EnumDescription2Dict<T>()
        {
 
            Dictionary<int, string> resultList = new Dictionary<int, string>();
            Type type = typeof(T);
            var strList = GetNames<T>().ToList();
            foreach (string key in strList)
            {
                var e = (Enum)Enum.Parse(type, key);
                string val = Enum.Format(type, Enum.Parse(type, key), "d");
                resultList.Add(int.Parse(val), e.GetDescription());
            }
            return resultList;
        }
    }
}