Get Enum from Description attribute
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Finding an enum value by its Description Attribute
我有一个从
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | enum Animal { [Description("")] NotSet = 0, [Description("Giant Panda")] GiantPanda = 1, [Description("Lesser Spotted Anteater")] LesserSpottedAnteater = 2 } public static string GetDescription(this Enum value) { FieldInfo field = value.GetType().GetField(value.ToString()); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; return attribute == null ? value.ToString() : attribute.Description; } |
所以我可以…
1 | string myAnimal = Animal.GiantPanda.GetDescription(); // ="Giant Panda" |
现在,我试着从另一个方向,求出等价函数,比如…
1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public static class EnumEx { public static T GetValueFromDescription<T>(string description) { var type = typeof(T); if(!type.IsEnum) throw new InvalidOperationException(); foreach(var field in type.GetFields()) { var attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if(attribute != null) { if(attribute.Description == description) return (T)field.GetValue(null); } else { if(field.Name == description) return (T)field.GetValue(null); } } throw new ArgumentException("Not found.","description"); // or return default(T); } } |
用途:
1 | var panda = EnumEx.GetValueFromDescription<Animal>("Giant Panda"); |
而不是扩展方法,只需尝试两个静态方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public static class Utility { public static string GetDescriptionFromEnumValue(Enum value) { DescriptionAttribute attribute = value.GetType() .GetField(value.ToString()) .GetCustomAttributes(typeof (DescriptionAttribute), false) .SingleOrDefault() as DescriptionAttribute; return attribute == null ? value.ToString() : attribute.Description; } public static T GetEnumValueFromDescription<T>(string description) { var type = typeof(T); if (!type.IsEnum) throw new ArgumentException(); FieldInfo[] fields = type.GetFields(); var field = fields .SelectMany(f => f.GetCustomAttributes( typeof(DescriptionAttribute), false), ( f, a) => new { Field = f, Att = a }) .Where(a => ((DescriptionAttribute)a.Att) .Description == description).SingleOrDefault(); return field == null ? default(T) : (T)field.Field.GetRawConstantValue(); } } |
在这里使用
1 2 3 4 | var result1 = Utility.GetDescriptionFromEnumValue( Animal.GiantPanda); var result2 = Utility.GetEnumValueFromDescription<Animal>( "Lesser Spotted Anteater"); |
除非您有Web服务,否则该解决方案工作得很好。
您需要执行以下操作,因为描述属性不可序列化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [DataContract] public enum ControlSelectionType { [EnumMember(Value ="Not Applicable")] NotApplicable = 1, [EnumMember(Value ="Single Select Radio Buttons")] SingleSelectRadioButtons = 2, [EnumMember(Value ="Completely Different Display Text")] SingleSelectDropDownList = 3, } public static string GetDescriptionFromEnumValue(Enum value) { EnumMemberAttribute attribute = value.GetType() .GetField(value.ToString()) .GetCustomAttributes(typeof(EnumMemberAttribute), false) .SingleOrDefault() as EnumMemberAttribute; return attribute == null ? value.ToString() : attribute.Value; } |
应该是非常简单的,这与你以前的方法正好相反;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static int GetEnumFromDescription(string description, Type enumType) { foreach (var field in enumType.GetFields()) { DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))as DescriptionAttribute; if(attribute == null) continue; if(attribute.Description == description) { return (int) field.GetValue(null); } } return 0; } |
用途:
1 |
不能扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public static class EnumHelper { public static T GetEnumFromString<T>(string value) { if (Enum.IsDefined(typeof(T), value)) { return (T)Enum.Parse(typeof(T), value, true); } else { string[] enumNames = Enum.GetNames(typeof(T)); foreach (string enumName in enumNames) { object e = Enum.Parse(typeof(T), enumName); if (value == GetDescription((Enum)e)) { return (T)e; } } } throw new ArgumentException("The value '" + value +"' does not match a valid enum name or description."); } } |
它的用法是这样的:
1 | Animal giantPanda = EnumHelper.GetEnumFromString<Animal>("Giant Panda"); |
您需要遍历animal中的所有枚举值,并返回与所需描述匹配的值。