/// <summary> /// EnumのDescriptionを取得 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="code"></param> /// <param name="para"></param> /// <returns></returns> public static List <EnumItem> GetList(this Type enumType, bool flgRemoveAll = false) { List <EnumItem> result = new List <EnumItem>(); foreach (var e in Enum.GetValues(enumType)) { EnumItem item = new EnumItem(); item.Index = (int)e; if (flgRemoveAll && item.Index == 0) { continue; } // Description object[] objArr = null; switch (App.Language) { case EnumLanguage.CN: objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true); if (objArr != null && objArr.Length > 0) { DescriptionAttribute da = objArr[0] as DescriptionAttribute; item.Description = da.Description; } break; case EnumLanguage.EN: objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(EnglishAttribute), true); if (objArr != null && objArr.Length > 0) { EnglishAttribute da = objArr[0] as EnglishAttribute; item.Description = da.Value; } break; default: objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(JapaneseAttribute), true); if (objArr != null && objArr.Length > 0) { JapaneseAttribute da = objArr[0] as JapaneseAttribute; item.Description = da.Value; } break; } // DBValue object[] dbValueArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(ValueAttribute), true); if (dbValueArr != null && dbValueArr.Length > 0) { ValueAttribute da = dbValueArr[0] as ValueAttribute; item.Value = da.Value; } result.Add(item); } return(result); }
/// <summary> /// 枚举Lsit信息获取 /// </summary> /// <param name="enumType"></param> /// <param name="flgRemoveAll">如果不想要All,设置为true</param> /// <returns></returns> public static List <EnumItem> GetList(this Type enumType, bool flgRemoveAll = false, EnumLanguage lan = EnumLanguage.CN) { // 返回值 List <EnumItem> result = new List <EnumItem>(); // 枚举遍历 foreach (var e in Enum.GetValues(enumType)) { EnumItem item = new EnumItem(); // 获取Index item.Index = (int)e; // 是否加入All枚举 if (flgRemoveAll && item.Index == 0) { continue; } // 获取描述 object[] objArr; switch (lan) { // 中文 case EnumLanguage.CN: objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true); if (objArr != null && objArr.Length > 0) { // 中文获取Description特性 DescriptionAttribute da = objArr[0] as DescriptionAttribute; item.Description = da.Description; } break; // 英文 case EnumLanguage.EN: objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(EnglishAttribute), true); if (objArr != null && objArr.Length > 0) { // 英文获取Description特性 EnglishAttribute da = objArr[0] as EnglishAttribute; item.Description = da.Value; } break; // 日文 default: objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(JapaneseAttribute), true); if (objArr != null && objArr.Length > 0) { // 日文获取Japanese特性 JapaneseAttribute da = objArr[0] as JapaneseAttribute; item.Description = da.Value; } break; } // 获取Value值 object[] dbValueArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(ValueAttribute), true); if (dbValueArr != null && dbValueArr.Length > 0) { ValueAttribute da = dbValueArr[0] as ValueAttribute; item.Value = da.Value; } result.Add(item); } return(result); }