现在的位置: 首页 > 综合 > 正文

C# 枚举的一些转换方法

2012年01月14日 ⁄ 综合 ⁄ 共 2777字 ⁄ 字号 评论关闭
View Code

 1     #region 枚举公用转换类
 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[] fis = value.GetFields();
25             foreach (FieldInfo fi in fis)
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 ConvertTo(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.ConvertTo(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 枚举相关方法
 2     public static class EnumHelper
 3     {
 4         /// <summary>
 5         /// 根据枚举类型获取字典
 6         /// </summary>
 7         /// <param name="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 枚举定义
 2     public enum CHMSO
 3     {
 4         [Description("kbro")]
 5         KBRO = 5301001,
 6         [Description("TFM")]
 7         TFM = 5301002,
 8 
 9     }
10     public enum CArea
11     {
12         [Description("大台北")]
13         BigTaipei = 5310,
14         [Description("中部")]
15         Center = 5311,
16         [Description("南部")]
17         South = 5312,
18         [Description("其他北部")]
19         OtherNorth = 5313,
20     }

 

抱歉!评论已关闭.