| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace OASystem.Domain.Enums
- {
- public static class EnumHelper
- {
- //--extension
- /// <summary>
- /// 获取当前枚举描述
- /// </summary>
- /// <param name="enumValue"></param>
- /// <returns></returns>
- public static string GetEnumDescription(this Enum enumValue)
- {
- try
- {
- Type type = enumValue.GetType();
- MemberInfo[] memInfo = type.GetMember(enumValue.ToString());
- if (null != memInfo && memInfo.Length > 0)
- {
- object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
- if (null != attrs && attrs.Length > 0)
- return ((DescriptionAttribute)attrs[0]).Description;
- }
- return enumValue.ToString();
- }
- catch (Exception)
- {
- return "";
- }
- }
- //根据值获取枚举方法
- public static Enum GetEnumByValue(Type enumType, string value)
- {
- return Enum.Parse(enumType, value) as Enum;
- }
- /// <summary>
- /// 根据Key获取枚举描述
- /// </summary>
- /// <param name="en"></param>
- /// <returns></returns>
- public static string GetDescription(this System.Enum en)
- {
- Type type = en.GetType();
- MemberInfo[] memInfo = type.GetMember(en.ToString());
- if (memInfo != null && memInfo.Length > 0)
- {
- object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
- if (attrs != null && attrs.Length > 0)
- return ((DescriptionAttribute)attrs[0]).Description;
- }
- return en.ToString();
- }
- /// <summary>
- /// 根据Key获取枚举描述
- /// </summary>
- /// <param name="en"></param>
- /// <returns></returns>
- public static string GetDescriptionLabel(this System.Enum en)
- {
- Type type = en.GetType();
- MemberInfo[] memInfo = type.GetMember(en.ToString());
- if (memInfo != null && memInfo.Length > 0)
- {
- object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
- if (attrs != null && attrs.Length > 0)
- return ((DescriptionAttribute)attrs[0]).Description;
- }
- return en.ToString();
- }
- /// <summary>
- /// 根据枚举类型得到其所有的 值 与 枚举定义Description属性 的集合
- /// </summary>
- /// <param name="enumType"></param>
- /// <returns></returns>
- public static NameValueCollection GetNVCFromEnumValue(Type enumType)
- {
- NameValueCollection nvc = new NameValueCollection();
- Type typeDescription = typeof(DescriptionAttribute);
- System.Reflection.FieldInfo[] fields = enumType.GetFields();
- string strText = string.Empty;
- string strValue = string.Empty;
- foreach (FieldInfo field in fields)
- {
- if (field.FieldType.IsEnum)
- {
- //strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
- strValue = (enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null) as int?)?.ToString() ?? string.Empty;
- object[] arr = field.GetCustomAttributes(typeDescription, true);
- if (arr.Length > 0)
- {
- DescriptionAttribute aa = (DescriptionAttribute)arr[0];
- strText = aa.Description;
- }
- else
- {
- strText = "";
- }
- nvc.Add(strValue, strText);
- }
- }
- return nvc;
- }
- /// <summary>
- /// 获取枚举的所有值及其对应的描述
- /// </summary>
- /// <param name="enumType"></param>
- /// <param name="enumKeys"></param>
- /// <returns></returns>
- public static Dictionary<string, string> GetEnumDescriptions(Type enumType, string[]? enumKeys = null)
- {
- var descriptions = new Dictionary<string, string>();
- if (!enumType.IsEnum)
- {
- return descriptions;
- //throw new ArgumentException("Type must be an enum", nameof(enumType));
- }
- foreach (var field in enumType.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
- {
- var descriptionAttribute = field.GetCustomAttribute<DescriptionAttribute>();
- var description = descriptionAttribute?.Description ?? field.Name;
- var name = field.Name;
- if (enumKeys != null && enumKeys.Any())
- {
- if (!enumKeys.Contains(name))
- {
- descriptions[name] = description;
- }
- }
- else {
- descriptions[name] = description;
- }
- }
- return descriptions;
- }
- /// <summary>
- /// 获取枚举的所有项(值、名称、描述)
- /// </summary>
- public static List<EnumItem> GetEnumItems<T>() where T : Enum
- {
- var items = new List<EnumItem>();
- var type = typeof(T);
- var values = Enum.GetValues(type);
- foreach (Enum value in values)
- {
- var field = type.GetField(value.ToString());
- var description = field?.GetCustomAttribute<DescriptionAttribute>()?.Description;
- items.Add(new EnumItem
- {
- Value = Convert.ToInt32(value),
- Name = value.ToString(),
- Description = description ?? value.ToString()
- });
- }
- return items;
- }
- /// <summary>
- /// 将枚举转换为下拉列表数据源
- /// </summary>
- public static List<SelectListItem> ToSelectList<T>(bool addDefault = false, string defaultText = "请选择") where T : Enum
- {
- var list = new List<SelectListItem>();
- if (addDefault)
- {
- list.Add(new SelectListItem { Value = -1, Text = defaultText });
- }
- foreach (var item in GetEnumItems<T>())
- {
- list.Add(new SelectListItem
- {
- Value = item.Value,
- Text = item.Description
- });
- }
- return list;
- }
- // 辅助类
- public class EnumItem
- {
- public int Value { get; set; }
- public string Name { get; set; }
- public string Description { get; set; }
- }
- // MVC 中的 SelectListItem
- public class SelectListItem
- {
- public int Value { get; set; }
- public string Text { get; set; }
- }
- }
- }
|