Esempio n. 1
0
        /// <summary>
        /// 获取枚举值列表,并转化为键值对
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="isHasAll">是否包含“全部”</param>
        /// <param name="filterItem">过滤项</param>
        /// <returns></returns>
        public static List <EnumKeyValue> EnumToList <T>(bool isHasAll, params string[] filterItem)
        {
            List <EnumKeyValue> list = new List <EnumKeyValue>();

            // 如果包含全部则添加
            if (isHasAll)
            {
                list.Add(new EnumKeyValue()
                {
                    Key = 0, Name = "全部"
                });
            }

            foreach (int item in Enum.GetValues(typeof(T)))
            {
                string name = Enum.GetName(typeof(T), item);
                // 跳过过滤项
                if (Array.IndexOf <string>(filterItem, name) != -1)
                {
                    continue;
                }
                // 添加
                EnumKeyValue model = new EnumKeyValue();
                model.Key  = item;
                model.Name = name;
                list.Add(model);
            }

            return(list);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取枚举描述列表,并转化为键值对
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="isHasAll">是否包含“全部”</param>
        /// <param name="filterItem">过滤项</param>
        /// <returns></returns>
        public static List <EnumKeyValue> EnumDescToList <T>(bool isHasAll, params string[] filterItem)
        {
            List <EnumKeyValue> list = new List <EnumKeyValue>();

            // 如果包含全部则添加
            if (isHasAll)
            {
                list.Add(new EnumKeyValue()
                {
                    Key = 0, Name = "全部"
                });
            }

            #region 方式一
            foreach (var item in typeof(T).GetFields())
            {
                // 获取描述
                var attr = item.GetCustomAttribute(typeof(DescriptionAttribute), true) as DescriptionAttribute;
                if (attr != null && !string.IsNullOrEmpty(attr.Description))
                {
                    // 跳过过滤项
                    if (Array.IndexOf <string>(filterItem, attr.Description) != -1)
                    {
                        continue;
                    }
                    // 添加
                    EnumKeyValue model = new EnumKeyValue();
                    model.Key  = (int)Enum.Parse(typeof(T), item.Name);
                    model.Name = attr.Description;
                    list.Add(model);
                }
            }
            #endregion

            #region 方式二
            //foreach (int item in Enum.GetValues(typeof(T)))
            //{
            //    // 获取描述
            //    FieldInfo fi = typeof(T).GetField(Enum.GetName(typeof(T), item));
            //    var attr = fi.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
            //    if (attr != null && !string.IsNullOrEmpty(attr.Description))
            //    {
            //        // 跳过过滤项
            //        if (Array.IndexOf<string>(filterItem, attr.Description) != -1)
            //        {
            //            continue;
            //        }
            //        // 添加
            //        EnumKeyValue model = new EnumKeyValue();
            //        model.Key = item;
            //        model.Name = attr.Description;
            //        list.Add(model);
            //    }
            //}
            #endregion

            return(list);
        }