1
using System;
2
using System.Collections;
3
using System.Text;
4
using System.ComponentModel;
5
using System.Reflection;
6
7
namespace Clubank.Common
8
{
9
/// <summary>
10
/// 提供了获得枚举的Description特性的实用方法以及可用于设计器的TypeConverter类
11
/// </summary>
12
public class EnumConverter : System.ComponentModel.EnumConverter
13
{
14
protected System.Type myVal;
15
16
/// <summary>
17
/// 获得枚举值的Description特性的值,一般是消息的搜索码
18
/// </summary>
19
/// <param name="value">要查找特性的枚举值</param>
20
/// <returns>返回查找到的Description特性的值,如果没有,就返回.ToString()</returns>
21
public static string GetEnumDescription(Enum value)
22
{
23
FieldInfo fi = value.GetType().GetField(value.ToString());
24
DescriptionAttribute[] attributes =
25
(DescriptionAttribute[])fi.GetCustomAttributes(
26
typeof(DescriptionAttribute), false);
27
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
28
}
29
30
/// <summary>
31
/// 根据特定的枚举值名称获得枚举值的Description特性的值
32
/// </summary>
33
/// <param name="value">枚举类型</param>
34
/// <param name="name">枚举值的名称</param>
35
/// <returns>返回查找到的Description特性的值,如果没有,就返回.ToString()</returns>
36
public static string GetEnumDescription(System.Type value, string name)
37
{
38
FieldInfo fi = value.GetField(name);
39
DescriptionAttribute[] attributes =
40
(DescriptionAttribute[])fi.GetCustomAttributes(
41
typeof(DescriptionAttribute), false);
42
return (attributes.Length > 0) ? attributes[0].Description : name;
43
}
44
45
/// <summary>
46
/// 基于Description特性或者枚举值名称获得具体的枚举值
47
/// </summary>
48
/// <param name="value">枚举类型</param>
49
/// <param name="description">Description特性值或者元素的名称</param>
50
/// <returns>返回枚举值。如果没找到,返回传入的Description值</returns>
51
public static object GetEnumValue(System.Type value, string description)
52
{
53
FieldInfo[] fis = value.GetFields();
54
foreach (FieldInfo fi in fis)
55
{
56
DescriptionAttribute[] attributes =
57
(DescriptionAttribute[])fi.GetCustomAttributes(
58
typeof(DescriptionAttribute), false);
59
if (attributes.Length > 0)
60
{
61
if (attributes[0].Description == description)
62
{
63
return fi.GetValue(fi.Name);
64
}
65
}
66
if (fi.Name == description)
67
{
68
return fi.GetValue(fi.Name);
69
}
70
}
71
return description;
72
}
73
74
public EnumConverter(System.Type type)
75
: base(type.GetType())
76
{
77
myVal = type;
78
}
79
80
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
81
{
82
if (value is Enum && destinationType == typeof(string))
83
{
84
return GetEnumDescription((Enum)value);
85
}
86
if (value is string && destinationType == typeof(string))
87
{
88
return GetEnumDescription(myVal, (string)value);
89
}
90
return base.ConvertTo(context, culture, value, destinationType);
91
}
92
93
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
94
{
95
if (value is string)
96
{
97
return GetEnumValue(myVal, (string)value);
98
}
99
if (value is Enum)
100
{
101
return GetEnumDescription((Enum)value);
102
}
103
return base.ConvertFrom(context, culture, value);
104
}
105
106
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
107
{
108
return true;
109
}
110
111
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
112
{
113
ArrayList values = new ArrayList();
114
FieldInfo[] fis = myVal.GetFields();
115
foreach (FieldInfo fi in fis)
116
{
117
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
118
typeof(DescriptionAttribute), false);
119
if (attributes.Length > 0)
120
values.Add(fi.GetValue(fi.Name));
121
}
122
return new TypeConverter.StandardValuesCollection(values);
123
}
124
125
}
126
}
127
using System;2
using System.Collections;3
using System.Text;4
using System.ComponentModel;5
using System.Reflection;6

7
namespace Clubank.Common8
{9
/// <summary>10
/// 提供了获得枚举的Description特性的实用方法以及可用于设计器的TypeConverter类11
/// </summary>12
public class EnumConverter : System.ComponentModel.EnumConverter13
{14
protected System.Type myVal;15

16
/// <summary>17
/// 获得枚举值的Description特性的值,一般是消息的搜索码18
/// </summary>19
/// <param name="value">要查找特性的枚举值</param>20
/// <returns>返回查找到的Description特性的值,如果没有,就返回.ToString()</returns>21
public static string GetEnumDescription(Enum value)22
{23
FieldInfo fi = value.GetType().GetField(value.ToString());24
DescriptionAttribute[] attributes =25
(DescriptionAttribute[])fi.GetCustomAttributes(26
typeof(DescriptionAttribute), false);27
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();28
}29

30
/// <summary>31
/// 根据特定的枚举值名称获得枚举值的Description特性的值32
/// </summary>33
/// <param name="value">枚举类型</param>34
/// <param name="name">枚举值的名称</param>35
/// <returns>返回查找到的Description特性的值,如果没有,就返回.ToString()</returns>36
public static string GetEnumDescription(System.Type value, string name)37
{38
FieldInfo fi = value.GetField(name);39
DescriptionAttribute[] attributes =40
(DescriptionAttribute[])fi.GetCustomAttributes(41
typeof(DescriptionAttribute), false);42
return (attributes.Length > 0) ? attributes[0].Description : name;43
}44

45
/// <summary>46
/// 基于Description特性或者枚举值名称获得具体的枚举值47
/// </summary>48
/// <param name="value">枚举类型</param>49
/// <param name="description">Description特性值或者元素的名称</param>50
/// <returns>返回枚举值。如果没找到,返回传入的Description值</returns>51
public static object GetEnumValue(System.Type value, string description)52
{53
FieldInfo[] fis = value.GetFields();54
foreach (FieldInfo fi in fis)55
{56
DescriptionAttribute[] attributes =57
(DescriptionAttribute[])fi.GetCustomAttributes(58
typeof(DescriptionAttribute), false);59
if (attributes.Length > 0)60
{61
if (attributes[0].Description == description)62
{63
return fi.GetValue(fi.Name);64
}65
}66
if (fi.Name == description)67
{68
return fi.GetValue(fi.Name);69
}70
}71
return description;72
}73

74
public EnumConverter(System.Type type)75
: base(type.GetType())76
{77
myVal = type;78
}79

80
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)81
{82
if (value is Enum && destinationType == typeof(string))83
{84
return GetEnumDescription((Enum)value);85
}86
if (value is string && destinationType == typeof(string))87
{88
return GetEnumDescription(myVal, (string)value);89
}90
return base.ConvertTo(context, culture, value, destinationType);91
}92

93
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)94
{95
if (value is string)96
{97
return GetEnumValue(myVal, (string)value);98
}99
if (value is Enum)100
{101
return GetEnumDescription((Enum)value);102
}103
return base.ConvertFrom(context, culture, value);104
}105

106
public override bool GetPropertiesSupported(ITypeDescriptorContext context)107
{108
return true;109
}110

111
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)112
{113
ArrayList values = new ArrayList();114
FieldInfo[] fis = myVal.GetFields();115
foreach (FieldInfo fi in fis)116
{117
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(118
typeof(DescriptionAttribute), false);119
if (attributes.Length > 0)120
values.Add(fi.GetValue(fi.Name));121
}122
return new TypeConverter.StandardValuesCollection(values);123
}124

125
}126
}127



浙公网安备 33010602011771号