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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** 1. 功能:枚举操作类,枚举类型创建,转换 * 2. 作者:杨磊 * 3. 创建日期:2008-1-30 * 4. 最后修改日期:2008-11-10 **/ using System; using System.Collections.Generic; using System.Text; using System.Collections; /// /// 枚举操作类,枚举类型创建,转换 /// public class EnumHelper { #region 通过字符串获取枚举成员实例 /// /// 通过字符串获取枚举成员实例 /// /// 枚举名,比如Enum1 /// 枚举成员的常量名或常量值, /// 范例:Enum1枚举有两个成员A=0,B=1,则传入"A"或"0"获取 Enum1.A 枚举类型 public static T GetInstance(string member) { return CommFun.ConvertTo(Enum.Parse(typeof(T), member, true)); } #endregion #region 获取枚举成员名称和成员值的键值对集合 /// /// 获取枚举成员名称和成员值的键值对集合 /// /// 枚举名,比如Enum1 public static Hashtable GetMemberKeyValue() { //创建哈希表 Hashtable ht = new Hashtable(); //获取枚举所有成员名称 string[] memberNames = GetMemberNames(); //遍历枚举成员 foreach (string memberName in memberNames) { ht.Add(memberName, GetMemberValue(memberName)); } //返回哈希表 return ht; } #endregion #region 获取枚举所有成员名称 /// /// 获取枚举所有成员名称 /// /// 枚举名,比如Enum1 public static string[] GetMemberNames() { return Enum.GetNames(typeof(T)); } #endregion #region 获取枚举成员的名称 /// /// 获取枚举成员的名称 /// /// 枚举名,比如Enum1 /// 枚举成员实例或成员值, /// 范例:Enum1枚举有两个成员A=0,B=1,则传入Enum1.A或0,获取成员名称"A" public static string GetMemberName(object member) { //转成基础类型的成员值 Type underlyingType = GetUnderlyingType(typeof(T)); object memberValue = CommFun.ConvertTo(member,underlyingType); //获取枚举成员的名称 return Enum.GetName(typeof(T), memberValue); } #endregion #re
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值