/// <summary> /// 把枚举转换list 并获取对应的值 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static List <EnumberEntity> EnumToList <T>() { List <EnumberEntity> list = new List <EnumberEntity>(); foreach (var e in Enum.GetValues(typeof(T))) { EnumberEntity m = new EnumberEntity(); object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true); if (objArr.Length > 0) { DescriptionAttribute da = objArr[0] as DescriptionAttribute; if (da != null) { m.Desction = da.Description; } } object[] objArr1 = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DisplayAttribute), true); if (objArr1.Length > 0) { DisplayAttribute da = objArr1[0] as DisplayAttribute; if (da != null) { m.EnumDisplay = da.Name; } } m.EnumValue = Convert.ToInt32(e); m.EnumName = e.ToString(); list.Add(m); } return(list); }
public static List <EnumberEntity> EnumToList(Type value) { var enumType = value.GetType(); //if (!enumType.IsEnum) return null; List <EnumberEntity> list = new List <EnumberEntity>(); foreach (var e in Enum.GetValues(value)) { EnumberEntity m = new EnumberEntity(); string name = e.ToString(); DescriptionAttribute customAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(value.GetField(name), typeof(DescriptionAttribute)); m.Desction = ((customAttribute == null) ? name : customAttribute.Description); m.EnumType = value.Name; m.Value = Convert.ToInt32(e); m.Name = e.ToString(); list.Add(m); } return(list); }
/// <summary> /// 枚举转List<> /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static List <EnumberEntity> EnumToList <T>() { List <EnumberEntity> list = new List <EnumberEntity>(); foreach (var e in Enum.GetValues(typeof(T))) { EnumberEntity m = new EnumberEntity(); Type enumType = typeof(T); if (enumType.IsEnum) { string name = e.ToString(); DescriptionAttribute customAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType.GetField(name), typeof(DescriptionAttribute)); m.Desction = ((customAttribute == null) ? name : customAttribute.Description); } m.Value = Convert.ToInt32(e); m.Name = e.ToString(); list.Add(m); } return(list); }