C# 枚举 Enum 工具类

public static class EnumUtil
    {
        /// <summary>
        /// 枚举子项实体
        /// </summary>
        public class EnumItem
        {
            /// <summary>
            /// 子项名称
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            /// 子项描述标注
            /// </summary>
            public string Description { get; set; }
            /// <summary>
            /// 子项 int 值
            /// </summary>
            public int IntValue { get; set; }
        }


        public class EnumItem<T> : EnumItem where T : Attribute
        {
            /// <summary>
            /// 自定义扩展
            /// </summary>
            public T Attribute { get; set; }
        }


        /// <summary>
        /// 获取枚举子项列表
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static List<EnumItem> GetEnumListItem(this Type enumType)
        {
            List<EnumItem> result = new List<EnumItem>();


            //获取枚举类型的所有子项
            //排除 名为 value__ 的项,这个是系统保留项,不处理。
            FieldInfo[] fields = enumType.GetFields().Where(s => s.Name != "value__").ToArray();


            foreach (FieldInfo field in fields)
            {
                EnumItem item = new EnumItem();


                //子项名字
                item.Name = field.Name;


                //获取标注
                DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (EnumAttributes != null && EnumAttributes.Length > 0)
                {
                    item.Description = EnumAttributes[0].Description;
                }


                //获取 int 值
                item.IntValue = (int)System.Enum.Parse(enumType, field.Name);


                result.Add(item);
            }


            return result;
        }


        public static List<EnumItem<T>> GetEnumListItem<T>(this Type enumType) where T : Attribute
        {
            List<EnumItem<T>> result = new List<EnumItem<T>>();


            //获取枚举类型的所有子项
            //排除 名为 value__ 的项,这个是系统保留项,不处理。
            FieldInfo[] fields = enumType.GetFields().Where(s => s.Name != "value__").ToArray();


            foreach (FieldInfo field in fields)
            {
                EnumItem<T> item = new EnumItem<T>();


                //子项名字
                item.Name = field.Name;


                //获取标注
                DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (EnumAttributes != null && EnumAttributes.Length > 0)
                {
                    item.Description = EnumAttributes[0].Description;
                }


                //获取自定义标注
                T[] customAttributes = (T[])field.GetCustomAttributes(typeof(T), false);
                if (customAttributes != null && customAttributes.Length > 0)
                {
                    item.Attribute = customAttributes as T;
                }


                //获取 int 值
                item.IntValue = (int)System.Enum.Parse(enumType, field.Name);


                result.Add(item);
            }


            return result;
        }




        public static EnumItem<T> GetEnum<T>(this Type enumType, string nameOrDescript) where T : Attribute
        {
            EnumItem<T> result = null;
            var list = GetEnumListItem<T>(enumType);
            foreach (var item in list) 
            {
                if (item.Name == nameOrDescript || item.Description == nameOrDescript) 
                {
                    result = item;
                    break;
                }
            }
            return result;
        }
        public static EnumItem<T> GetEnum<T>(this Type enumType, int value) where T : Attribute
        {
            EnumItem<T> result = null;
            var list = GetEnumListItem<T>(enumType);
            foreach (var item in list)
            {
                if (item.IntValue == value)
                {
                    result = item;
                    break;
                }
            }
            return result;
        }


        public static EnumItem GetEnum(this Type enumType, string nameOrDescript)
        {
            EnumItem result = null;
            var list = GetEnumListItem(enumType);
            foreach (var item in list)
            {
                if (item.Name == nameOrDescript || item.Description == nameOrDescript)
                {
                    result = item;
                    break;
                }
            }
            return result;
        }
        public static EnumItem GetEnum(this Type enumType, int value)
        {
            EnumItem result = null;
            var list = GetEnumListItem(enumType);
            foreach (var item in list)
            {
                if (item.IntValue == value)
                {
                    result = item;
                    break;
                }
            }
            return result;
        }




        /// <summary>
        /// 获取枚举的描述
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="enum_item_name"></param>
        /// <returns></returns>
        public static string GetEnumDescription(this Type enumType, string enum_item_name)
        {
            string result = string.Empty;
            if (ExistsEnumItem(enumType, enum_item_name))
            {
                //直接通过子项名字找出子项的反射类型。
                FieldInfo field = enumType.GetField(enum_item_name);
                //反射类型获取出子项上的标注
                DescriptionAttribute[] Descriptions = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (Descriptions != null && Descriptions.Length > 0)
                {
                    result = Descriptions[0].Description;
                }
            }
            return result;
        }
        /// <summary>
        /// 获取枚举的描述
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="enum_item_value"></param>
        /// <returns></returns>
        public static string GetEnumDescription(this Type enumType, int enum_item_value)
        {
            string result = string.Empty;
            FieldInfo[] fields = enumType.GetFields().Where(s => s.Name != "value__").ToArray();


            foreach (FieldInfo field in fields)
            {
                if ((int)System.Enum.Parse(enumType, field.Name) == enum_item_value)
                {
                    DescriptionAttribute[] Descriptions = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (Descriptions != null && Descriptions.Length > 0)
                    {
                        result = Descriptions[0].Description;
                    }
                    break;
                }
            }


            return result;
        }






        /// <summary>
        /// 判断给定的名字是否是枚举里的子项
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="enum_item_name"></param>
        /// <returns></returns>
        public static bool ExistsEnumItem(this Type enumType, string enum_item_name)
        {
            return enumType.GetField(enum_item_name) != null;
        }
        /// <summary>
        /// 判断给定的值是否是枚举里的子项的值
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="enum_item_value"></param>
        /// <returns></returns>
        public static bool ExistsEnumItem(this Type enumType, int enum_item_value)
        {
            bool result = false;
            FieldInfo[] fields = enumType.GetFields().Where(s => s.Name != "value__").ToArray();
            foreach (FieldInfo field in fields)
            {
                if ((int)System.Enum.Parse(enumType, field.Name) == enum_item_value)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值