Exemple #1
0
        /// <summary>
        /// 使用:
        ///     typeOf(IsEnable).GetEnumList();
        /// 获取枚举的Id和Name,包含枚举中的所有枚举项(不包括空项)
        /// </summary>
        /// <param name="enumType">枚举类型(需要查看的枚举类型typeof())</param>
        /// <returns></returns>
        public static IList <BaseEnumInf> GetEnumListInfo(this Type enumType)
        {
            if (!enumType.IsEnum) //首先判断传递进来的对象是否是枚举
            {
                throw new InvalidOperationException("传递的对象不是枚举项,抛出异常");
            }
            IList <BaseEnumInf> listBaseEnumInfs = new List <BaseEnumInf>();
            //获取枚举的Description特性(说明项)
            Type typeDescription = typeof(DescriptionAttribute);

            //获取所有的枚举字段
            FieldInfo[] fieldInfos = enumType.GetFields();
            foreach (var fieldInfo in fieldInfos) //循环得到的枚举对象
            {
                BaseEnumInf baseEnumInf = new BaseEnumInf();
                if (!fieldInfo.FieldType.IsEnum) //判断字段类型是否是枚举
                {
                    continue;
                }
                //获取枚举的Id信息
                baseEnumInf.Id = (int)enumType.InvokeMember(fieldInfo.Name, BindingFlags.GetField, null, null, null);
                //不包括空值
                if (baseEnumInf.Id >= 0)
                {
                    object[] arrayObjects = fieldInfo.GetCustomAttributes(typeDescription, false);
                    baseEnumInf.Name = arrayObjects.Length > 0
                        ? ((DescriptionAttribute)arrayObjects[0]).Description
                        : fieldInfo.Name;
                    //添加到列表中返回
                    listBaseEnumInfs.Add(baseEnumInf);
                }
            }
            return(listBaseEnumInfs);
        }
Exemple #2
0
 /// <summary>
 /// 使用:
 ///     typeOf(IsEnable).GetEnumList();
 /// 获取枚举的Id和Name,包含枚举中的所有枚举项(不包括空项)
 /// </summary>
 /// <param name="enumType">枚举类型(需要查看的枚举类型typeof())</param>
 /// <returns></returns>
 public static IList<BaseEnumInf> GetEnumListInfo(this Type enumType)
 {
     if (!enumType.IsEnum) //首先判断传递进来的对象是否是枚举
     {
         throw new InvalidOperationException("传递的对象不是枚举项,抛出异常");
     }
     IList<BaseEnumInf> listBaseEnumInfs = new List<BaseEnumInf>();
     //获取枚举的Description特性(说明项)
     Type typeDescription = typeof (DescriptionAttribute);
     //获取所有的枚举字段
     FieldInfo[] fieldInfos = enumType.GetFields();
     foreach (var fieldInfo in fieldInfos) //循环得到的枚举对象
     {
         BaseEnumInf baseEnumInf = new BaseEnumInf();
         if (!fieldInfo.FieldType.IsEnum) //判断字段类型是否是枚举
             continue;
         //获取枚举的Id信息
         baseEnumInf.Id = (int) enumType.InvokeMember(fieldInfo.Name, BindingFlags.GetField, null, null, null);
         //不包括空值
         if (baseEnumInf.Id >= 0)
         {
             object[] arrayObjects = fieldInfo.GetCustomAttributes(typeDescription, false);
             baseEnumInf.Name = arrayObjects.Length > 0
                 ? ((DescriptionAttribute) arrayObjects[0]).Description
                 : fieldInfo.Name;
             //添加到列表中返回
             listBaseEnumInfs.Add(baseEnumInf);
         }
     }
     return listBaseEnumInfs;
 }