EnumHelper.cs

网上找的,还比较实用的:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Linq;
  5 using System.Reflection;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 
  9 namespace Dapper.Tool
 10 {
 11     /// <summary>
 12     /// 枚举扩展方法类
 13     /// </summary>
 14     public class EnumHelper
 15     {
 16         /// <summary>
 17         /// 返回枚举值的描述信息。
 18         /// </summary>
 19         /// <param name="value">要获取描述信息的枚举值。</param>
 20         /// <returns>枚举值的描述信息。</returns>
 21         public static string GetEnumDesc<T>(object value)
 22         {
 23             Type enumType = typeof(T);
 24             DescriptionAttribute attr = null;
 25 
 26             // 获取枚举常数名称。
 27             string name = Enum.GetName(enumType, value);
 28             if (name != null)
 29             {
 30                 // 获取枚举字段。
 31                 FieldInfo fieldInfo = enumType.GetField(name);
 32                 if (fieldInfo != null)
 33                 {
 34                     // 获取描述的属性。
 35                     attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
 36                 }
 37             }
 38 
 39             // 返回结果
 40             if (!string.IsNullOrEmpty(attr?.Description))
 41                 return attr.Description;
 42 
 43             return string.Empty;
 44         }
 45 
 46         /// <summary>
 47         /// 返回枚举项的描述信息。
 48         /// </summary>
 49         /// <param name="e">要获取描述信息的枚举项。</param>
 50         /// <returns>枚举项的描述信息。</returns>
 51         public static string GetEnumDesc(Enum e)
 52         {
 53             if (e == null)
 54                 return string.Empty;
 55             
 56             Type enumType = e.GetType();
 57             DescriptionAttribute attr = null;
 58 
 59             // 获取枚举字段。
 60             FieldInfo fieldInfo = enumType.GetField(e.ToString());
 61             if (fieldInfo != null)
 62             {
 63                 // 获取描述的属性。
 64                 attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
 65             }
 66 
 67             // 返回结果
 68             if (!string.IsNullOrEmpty(attr?.Description))
 69                 return attr.Description;
 70 
 71             return string.Empty;
 72         }
 73 
 74 
 75         /// <summary>
 76         /// 获取枚举描述列表,并转化为键值对
 77         /// </summary>
 78         /// <typeparam name="T"></typeparam>
 79         /// <param name="isHasAll">是否包含“全部”</param>
 80         /// <param name="filterItem">过滤项</param>
 81         /// <returns></returns>
 82         public static List<EnumKeyValue> EnumDescToList<T>(bool isHasAll, params string[] filterItem)
 83         {
 84             List<EnumKeyValue> list = new List<EnumKeyValue>();
 85 
 86             // 如果包含全部则添加
 87             if (isHasAll)
 88             {
 89                 list.Add(new EnumKeyValue() { Key = 0, Name = "全部" });
 90             }
 91 
 92             #region 方式一
 93             foreach (var item in typeof(T).GetFields())
 94             {
 95                 // 获取描述
 96                 var attr = item.GetCustomAttribute(typeof(DescriptionAttribute), true) as DescriptionAttribute;
 97                 if (!string.IsNullOrEmpty(attr?.Description))
 98                 {
 99                     // 跳过过滤项
100                     if (Array.IndexOf<string>(filterItem, attr.Description) != -1)
101                     {
102                         continue;
103                     }
104                     // 添加
105                     EnumKeyValue model = new EnumKeyValue
106                     {
107                         Key = (int) Enum.Parse(typeof (T), item.Name),
108                         Name = attr.Description
109                     };
110 
111                     list.Add(model);
112                 }
113             }
114             #endregion
115 
116             #region 方式二
117             //foreach (int item in Enum.GetValues(typeof(T)))
118             //{
119             //    // 获取描述
120             //    FieldInfo fi = typeof(T).GetField(Enum.GetName(typeof(T), item));
121             //    var attr = fi.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
122             //    if (attr != null && !string.IsNullOrEmpty(attr.Description))
123             //    {
124             //        // 跳过过滤项
125             //        if (Array.IndexOf<string>(filterItem, attr.Description) != -1)
126             //        {
127             //            continue;
128             //        }
129             //        // 添加
130             //        EnumKeyValue model = new EnumKeyValue();
131             //        model.Key = item;
132             //        model.Name = attr.Description;
133             //        list.Add(model);
134             //    }
135             //} 
136             #endregion
137 
138             return list;
139         }
140 
141         /// <summary>
142         /// 获取枚举值列表,并转化为键值对
143         /// </summary>
144         /// <typeparam name="T"></typeparam>
145         /// <param name="isHasAll">是否包含“全部”</param>
146         /// <param name="filterItem">过滤项</param>
147         /// <returns></returns>
148         public static List<EnumKeyValue> EnumToList<T>(bool isHasAll, params string[] filterItem)
149         {
150             List<EnumKeyValue> list = new List<EnumKeyValue>();
151 
152             // 如果包含全部则添加
153             if (isHasAll)
154             {
155                 list.Add(new EnumKeyValue() { Key = 0, Name = "全部" });
156             }
157 
158             foreach (int item in Enum.GetValues(typeof(T)))
159             {
160                 string name = Enum.GetName(typeof(T), item);
161                 // 跳过过滤项
162                 if (Array.IndexOf<string>(filterItem, name) != -1)
163                 {
164                     continue;
165                 }
166                 // 添加
167                 EnumKeyValue model = new EnumKeyValue();
168                 model.Key = item;
169                 model.Name = name;
170                 list.Add(model);
171             }
172 
173             return list;
174         }
175 
176         /// <summary>
177         /// 枚举键值对
178         /// </summary>
179         public class EnumKeyValue
180         {
181             public int Key { get; set; }
182             public string Name { get; set; }
183         }
184     }
185 }
原文地址:https://www.cnblogs.com/LiuLiangXuan/p/7028263.html