枚举帮助类EnumHelper--NetCore3/.Net5

返回一个枚举值,该值指示指定枚举的描述名称中存在给定的整数值或其字符串名称,如若不存在则返回指定默认值

        /// 
        /// 返回一个枚举值,该值指示指定枚举的描述名称中存在给定的整数值或其字符串名称,如若不存在则返回指定默认值。
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T GetEnumByDescription(string value,T defaultValue) where T : struct, Enum
        {
            var list = GetEnumItems2();
            var m = list.FirstOrDefault(i => i.Description == value);
            if (m == null)
            {
                return defaultValue;
            }
            return m.Enum;
        }
        public static List> GetEnumItems2() where T : struct, Enum
        {
            List> itemList = new List>();
            foreach (var v in Enum.GetValues())
            {
                EnumItem2 item = new EnumItem2();
                item.EnumKey = Convert.ToInt32(v);
                item.EnumValue = v.ToString();
                item.Description = GetDescription(v);
                item.Enum = v;
                itemList.Add(item);
            }
            return itemList;
        }
        public class EnumItem2
        {
            public int EnumKey { get; set; }
            public string EnumValue { get; set; }
            public string Description { get; set; }
            public T Enum { get; set; }
        }

枚举转换下拉选项
SelectListItem 需要安装Microsoft.AspNetCore.Mvc.ViewFeatures并引用Microsoft.AspNetCore.Mvc.Rendering

       /// 
       /// 返回Razor可识别的下拉框集合
       /// 
       /// 
       /// 
       /// 
       /// 
        public static IList ToSelectList(Type enumType, int selectValue, params int[] removes)
        {
            IList listItem = new List();
            if (enumType.IsEnum)
            {
                Array values = Enum.GetValues(enumType);
                if (values.Length > 0)
                {
                    foreach (int item in values)
                    {
                        if (removes.Length > 0)
                        {
                            if (removes.Contains(item))
                            {
                                continue;
                            }
                        }

                        var test = System.Enum.GetName(enumType, item);
                        FieldInfo fi = enumType.GetField(test);
                        var dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
                        if (dna != null && !string.IsNullOrEmpty(dna.Description))
                        {
                            test = dna.Description;
                        }
                        listItem.Add(new SelectListItem { Value = item.ToString(), Text = test, Selected = selectValue == item });
                    }
                }
            }
            else
            {
                throw new ArgumentException("请传入正确的枚举!");
            }
            return listItem;
        }

获取枚举描述信息

        /// 
        /// 获取描述信息
        /// 
        /// 
        /// 
        public static string GetDescription(this Enum obj)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            try
            {
                Type enumType = obj.GetType();
                FieldInfo fi = enumType.GetField(System.Enum.GetName(enumType, obj));
                var dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
                if (dna != null && string.IsNullOrEmpty(dna.Description) == false)
                    return dna.Description;
            }
            catch
            {
                return string.Empty;
            }
            return obj.ToString();
        }

双属性描述设置

    /// 
    /// 枚举双描述属性
    /// 
    public class BaseDescriptionAttribute : DescriptionAttribute
    {
        public BaseDescriptionAttribute(string description1)   : base(description1)   {    }
        public BaseDescriptionAttribute(string description1, string description2)   : base(description1)
        {
            this.Description2 = description2;
        }
        public string Description2 { get; set; }
    }

双属性使用

 public enum AccessStatus
    {
        [BaseDescription("正常", "正常")]
        Normal,
    }

获取双属性描述信息

        /// 
        /// 双属性扩展方法,获得枚举的Description
        /// 
        /// 枚举值
        /// 当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用
        /// 枚举的Description
        public static EnumItem GetBaseDescription(this Enum value, bool nameInstead = true)
        {
            EnumItem e = new EnumItem();
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }
            FieldInfo field = type.GetField(name);
            BaseDescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(BaseDescriptionAttribute)) as BaseDescriptionAttribute;
            if (attribute == null && nameInstead == true)
            {
                e.Description1 = name;
                e.Description2 = name;
                return e;
            }
            else
            {
                e.Description1 = attribute.Description;
                e.Description2 = attribute.Description2;
                return e;
            }

        }
   public class EnumItem
    {
        public int EnumKey { get; set; }
        public string EnumValue { get; set; }
        public string Description1 { get; set; }
        public string Description2 { get; set; }
    }
        /// 
        /// 获取枚举的相关信息
        /// 
        /// 枚举的类型
        /// 
        public static List GetBaseEnumItems(Type e)
        {
            List itemList = new List();
            foreach (Enum v in Enum.GetValues(e))
            {
                EnumItem item = new EnumItem();
                item.EnumKey = Convert.ToInt32(v);
                item.EnumValue = v.ToString();
                item.Description1 = GetBaseDescription(v).Description1;
                item.Description2 = GetBaseDescription(v).Description2;
                itemList.Add(item);
            }
            return itemList;

        }

你可能感兴趣的:(枚举帮助类EnumHelper--NetCore3/.Net5)