示例#1
0
        /// <summary>
        /// 获得枚举的{EDescription,Value}字典集
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public static Dictionary <string, T> GetEDescription2ValueDictionary <T>(Func <T, bool> predicate = null)
        {
            var enumType = typeof(T);

            if (!enumType.IsEnum)
            {
                throw new ArgumentException("传入的泛型参数必须是枚举类型!", nameof(enumType));
            }
            var dic = new Dictionary <string, T>();

            var enumValues = Enum.GetValues(enumType).Cast <T>();

            if (predicate != null)
            {
                enumValues = enumValues.Where(predicate);
            }
            foreach (T enumValue in enumValues)
            {
                var       name  = Enum.GetName(enumType, enumValue);
                FieldInfo field = enumType.GetFieldInfo(name);

                EDescriptionAttribute attribute = field.GetCustomAttribute(typeof(EDescriptionAttribute)) as EDescriptionAttribute;
                var key = attribute == null ? name : attribute.EDescription;
                dic.Add(key, enumValue);
            }
            return(dic);
        }
示例#2
0
        /// <summary>
        /// 扩展方法,获得枚举的{Value,Description,EDescription}字典集
        /// 获取enum值对应多个的Description(需根据需求调整EnumItem)
        /// </summary>
        /// <param name="enumType">枚举本身</param>
        /// <param name="IgnoreValues">忽略值集合,默认为空</param>
        /// <returns>枚举的Description</returns>
        public static List <EnumItem> GetDescriptionToList(Type enumType, int[] IgnoreValues = null)
        {
            if (!enumType.IsEnum)
            {
                throw new ArgumentException("传入的参数必须是枚举类型!", nameof(enumType));
            }

            var list       = new List <EnumItem>();
            var enumValues = Enum.GetValues(enumType);

            foreach (Enum enumValue in enumValues)
            {
                //对应的int值
                var       valueNum = enumValue.GetHashCode();
                var       name     = Enum.GetName(enumType, enumValue);
                FieldInfo field    = enumType.GetFieldInfo(name);
                //中文描述
                DescriptionAttribute attributeN = field.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute;
                //英文描述
                EDescriptionAttribute attributeE = field.GetCustomAttribute(typeof(EDescriptionAttribute)) as EDescriptionAttribute;
                //中描述文本
                var valueN = attributeN == null ? string.Empty : attributeN.Description;
                //英中描述文本
                var valueE = attributeE == null ? string.Empty : attributeE.EDescription;
                if (IgnoreValues != null && IgnoreValues.Any())
                {
                    if (IgnoreValues.Contains(valueNum))
                    {
                        continue;
                    }
                }

                list.Add(new EnumItem(valueNum, valueN, valueE));
            }
            return(list);
        }