using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using System.Text; namespace XYY.Core.Standard.Mvc { public class EnumItem { public string Name { get; set; } public int Value { get; set; } } public class EnumItem2 { public string Name { get; set; } public int Id { get; set; } } public static class EnumsHelper { public static List GetEnumItemsDes() where T : Enum { List enumItems = new List(); var t = typeof(T); if (t.IsEnum) { var values = t.GetEnumValues(); foreach (var v in values) { FieldInfo field = t.GetField(t.GetEnumName(v)); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; enumItems.Add(new EnumItem2 { Id = (int)v, Name = attribute == null ? field.Name : attribute.Description }); } } return enumItems; } public static List GetEnumItems(string enumName) { List enumItems = new List(); string fileName = System.IO.Path.Combine(AppContext.BaseDirectory, "XYY.Model.Standard.dll"); var ass = Assembly.LoadFile(fileName); var t = ass.GetType("XYY.Model.Standard." + enumName); if (t != null) { if (t.IsEnum) { var values = t.GetEnumValues(); foreach (var v in values) { enumItems.Add(new EnumItem2 { Id = (int)v, Name = t.GetEnumName(v) }); } } } return enumItems; } public static List GetEnumItems(Type type) { string[] Names = Enum.GetNames(type); var valueAraay = Enum.GetValues(type); List list = new List(); for (int i = 0; i < Names.Length; i++) { list.Add(new EnumItem { Name = Names[i], Value = (int)valueAraay.GetValue(i) }); } return list; } public static List GetEnumItemsDes(string enumName) { List enumItems = new List(); string fileName = System.IO.Path.Combine(AppContext.BaseDirectory, "XYY.Model.Standard.dll"); var ass = Assembly.LoadFile(fileName); var t = ass.GetType("XYY.Model.Standard." + enumName); if (t != null) { if (t.IsEnum) { var values = t.GetEnumValues(); foreach (var v in values) { FieldInfo field = t.GetField(t.GetEnumName(v)); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; enumItems.Add(new EnumItem2 { Id = (int)v, Name = attribute == null ? "未知" : attribute.Description }); } } } return enumItems; } } }