View Code
1 # region enumeration public conversion class 2 public class EnumDescConverter: System. componentModel. enumConverter 3 {4 protected System. type m_MyVal; 5 public static string GetEnumDescription (Enum value) 6 {7 FieldInfo fi = value. getType (). getField (value. toString (); 8 DescriptionAttribute [] attributes = 9 (DescriptionAttribute []) fi. getCustomAttributes (10 typeof (DescriptionAttribute), false); 11 return (attributes. length> 0 )? Attributes [0]. description: value. toString (); 12} 13 14 public static string GetEnumDescription (System. type value, string name) 15 {16 FieldInfo fi = value. getField (name); 17 DescriptionAttribute [] attributes = 18 (DescriptionAttribute []) fi. getCustomAttributes (19 typeof (DescriptionAttribute), false); 20 return (attributes. length> 0 )? Attributes [0]. description: name; 21} 22 public static object GetEnumValue (System. type value, string description) 23 {24 FieldInfo [] FD = value. getFields (); 25 foreach (FieldInfo fi in FD) 26 {27 DescriptionAttribute [] attributes = (DescriptionAttribute []) fi. getCustomAttributes (typeof (DescriptionAttribute), false); 28 if (attributes. length> 0) 29 {30 if (attributes [0]. description = description) 31 {32 return fi. getValue (fi. name); 33} 34} 35 if (fi. name = description) 36 {37 return fi. getValue (fi. name); 38} 39} 40 return description; 41} 42 public EnumDescConverter (System. type type) 43: base (type. getType () 44 {45 m_MyVal = type; 46} 47 public override object Convertor (ITypeDescriptorContext context, System. globalization. cultureInfo culture, object value, Type destinationType) 48 {49 if (value is Enum & destinationType = typeof (string) 50 {51 return GetEnumDescription (Enum) value ); 52} 53 if (value is string & destinationType = typeof (string) 54 {55 return GetEnumDescription (m_MyVal, (string) value); 56} 57 return base. convertize (context, culture, value, destinationType); 58} 59 60 public override object ConvertFrom (ITypeDescriptorContext context, System. globalization. cultureInfo culture, object value) 61 {62 if (value is string) 63 {64 return GetEnumValue (m_MyVal, (string) value); 65} 66 if (value is Enum) 67 {68 return GetEnumDescription (Enum) value); 69} 70 return base. convertFrom (context, culture, value); 71} 72} 73 # endregion
View Code
1 # region enumeration related methods 2 public static class EnumHelper 3 {4 // <summary> 5 // obtain the dictionary 6 Based on the enumeration type /// </summary> 7 // /<param name = "type"> Enumeration type </param> 8 // <returns> </returns> 9 public static Dictionary <int, string> GetEnumeTypeDic (Type type) 10 {11 Dictionary <int, string> dic = new Dictionary <int, string> (); 12 int key =-1; 13 string value = string. empty; 14 foreach (object gc in Enum. getValues (type) 15 {16 Key = (int) gc; 17 value = EnumDescConverter. GetEnumDescription (Enum) gc); 18 if (key! =-1 &&! Dic. ContainsKey (key) 19 {20 dic. Add (key, value); 21} 22} 23 return dic; 24} 25} 26 # endregion
View Code
1 # region enumeration Definition 2 public enum CHMSO 3 {4 [Description ("kbro")] 5 KBRO = 5301001, 6 [Description ("TFM")] 7 TFM = 5301002, 8 9} 10 public enum CArea11 {12 [Description ("")] 13 BigTaipei = 5310,14 [Description ("")] 15 Center = 5311,16 [Description ("Southern")] 17 South = 5312,18 [Description ("other northern")] 19 OtherNorth = 5313,20}