项目中,用到枚举值,并且增加中英文描述。
一般的[Description]属性,无法满足中英文,所以进行了简单扩展。
继承DescriptionAttribute,增加了英文描述descriptionEN
public class BaseDescriptionAttribute : DescriptionAttribute { public BaseDescriptionAttribute(string descriptionCN) : base(descriptionCN) { } public BaseDescriptionAttribute(string descriptionCN, string descriptionEN) : base(descriptionCN) { this.descriptionEN = descriptionEN; } private string descriptionEN; public string DescriptionEN { get { return descriptionEN; } set { descriptionEN = value; } } }增加一个操作类: public static class EnumOperate
GetBaseDescription:操作BaseDescriptionAttribute
GetDescription:操作DescriptionAttribute
EnumToDictionary:获取枚举值的key和value
GetEnumItems:获取枚举的相关信息(key,value,中文描述,英文描述)
public static class EnumOperate { ///一些基本方法,参考了 http://www.cnblogs.com/lyl6796910/p/3958768.html/// 扩展方法,获得枚举的Description /// /// 枚举值 /// 当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用 ///枚举的Description public static EnumItem GetBaseDescription(this Enum value, Boolean nameInstead = true) { EnumItem e = new EnumItem(); Type type = value.GetType(); string name = Enum.GetName(type, value); if (name == null) { return null; } FieldInfo field = type.GetField(name); BaseDescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(BaseDescriptionAttribute)) as BaseDescriptionAttribute; if (attribute == null && nameInstead == true) { e.DescriptionCN = name; e.DescriptionEN = name; return e; } else { e.DescriptionCN = attribute.Description; e.DescriptionEN = attribute.DescriptionEN; return e; } } ////// 扩展方法,获得枚举的Description /// /// 枚举值 /// 当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用 ///枚举的Description public static string GetDescription(this Enum value, Boolean nameInstead = true) { Type type = value.GetType(); string name = Enum.GetName(type, value); if (name == null) { return null; } FieldInfo field = type.GetField(name); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute == null && nameInstead == true) { return name; } return attribute == null ? null : attribute.Description; } ////// 把枚举转换为键值对集合 /// 枚举转换为键值对集合 /// Dictionary /// 枚举类型 /// 获得值得文本 ///dic = EnumUtil.EnumToDictionary(typeof(Season), e => e.GetDescription()); /// 以枚举值为key,枚举文本为value的键值对集合 public static DictionaryEnumToDictionary(Type enumType, Func getText) { if (!enumType.IsEnum) { throw new ArgumentException("传入的参数必须是枚举类型!", "enumType"); } Dictionary enumDic = new Dictionary (); Array enumValues = Enum.GetValues(enumType); foreach (Enum enumValue in enumValues) { Int32 key = Convert.ToInt32(enumValue); String value = getText(enumValue); enumDic.Add(key, value); } return enumDic; } /// /// 获取枚举的相关信息 /// /// 枚举的类型 ///public static List GetEnumItems(Type e) { List itemList = new List (); foreach (Enum v in Enum.GetValues(e)) { EnumItem item = new EnumItem(); // TODO: 遍历操作 item.EnumKey = Convert.ToInt32(v); item.EnumValue = v.ToString(); item.DescriptionCN = GetBaseDescription(v).DescriptionCN; item.DescriptionEN = GetBaseDescription(v).DescriptionEN; itemList.Add(item); } return itemList; } public class EnumItem { public int EnumKey { get; set; } public string EnumValue { get; set; } public string DescriptionCN { get; set; } public string DescriptionEN { get; set; } } }
实际运用:
获取某个枚举类的全部信息
static void Main(string[] args) { var q = EnumOperate.GetEnumItems(typeof(WMS_Invoicing)); Console.ReadLine(); }public enum WMS_Invoicing { //入库 [TestApp.Program.BaseDescription("In","InEn")] Input = 1, //出库 [TestApp.Program.BaseDescription("Out", "OutEn")] Output = 2, //修正 [TestApp.Program.BaseDescription("Corrected", "123")] Corrected = 3, //损耗 [TestApp.Program.BaseDescription("Loss", "haha")] Loss = 4, }