什么叫类型转换器?
前篇文章中的为何没有用到, 其实它也用到了, 因为它用的是系统自带的类型int, 类型转换器已经由系统自动提供了.
如果我们使用了自己定义的类型, 因为系统中没有相应的类型转换器, 这就需要我们写一个类型转换器.
下面我们写一个稍稍复杂点的属性, 它是由简单的类型加简单的属性组合而成的,(没有晕吧),
也就是说我要自已定义一个类型, 而不用系统自带的类型(比如前篇文章中的int类型)
下面就是拥有一个简单的复杂属性的简单控件,
1
using System.ComponentModel;
2
using System.Windows.Forms;
3
using System.Drawing;
4
5
namespace CustomControlSample
6

{
7
public class SimpleComplexProperty : Control
8
{
9
private SimpleCustomType complexField;
10
11
[Category("我是复杂的属性哦!")]
12
[Description("我是简单的复杂属性,因为我是由简单的类型和简单的方式定义的。\n定义我的类型很简单,只有两个属性(Min, Max);定义我的body也很简单,只是简单的get, set.")]
13
public SimpleCustomType ComplexProperty
14
{
15
get
{ return complexField; }
16
set
{ complexField = value; }
17
}
18
19
protected override void OnPaint(PaintEventArgs e)
20
{
21
base.OnPaint(e);
22
e.Graphics.DrawRectangle(Pens.Red, new Rectangle(Point.Empty, new Size(Width - 1, Height - 1)));
23
}
24
25
}
26
27
简单的自定义类型#region 简单的自定义类型
28
/**//// <summary>
29
/// 简单的自定义类型
30
/// </summary>
31
public class SimpleCustomType
32
{
33
private int _min;
34
private int _max;
35
36
public int Min
37
{
38
get
{ return _min; }
39
set
{ _min = value; }
40
}
41
public int Max
42
{
43
get
{ return _max; }
44
set
{ _max = value; }
45
}
46
47
public SimpleCustomType()
{ }
48
49
public SimpleCustomType(int min, int max)
50
{
51
_min = min;
52
_max = max;
53
}
54
}
55
#endregion
56
57
简单的自定义类型的类型转换器#region 简单的自定义类型的类型转换器
58
/**//// <summary>
59
/// 简单的自定义类型的类型转换器
60
/// </summary>
61
public class SimpleCustomTypeConverter : TypeConverter
62
{
63
public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
64
{
65
// 允许字符串类型转换为我们自定义的类型
66
if (sourceType == typeof(string))
67
return true;
68
return base.CanConvertFrom(context, sourceType);
69
}
70
71
public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
72
{
73
// 允许我们自定义的类型转换为字符串类型
74
if (destinationType == typeof(string))
75
return true;
76
return base.CanConvertTo(context, destinationType);
77
}
78
79
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
80
{
81
// 字符串类型的值如何转换成我们自定义的类型呢?看下面
82
if (value is string)
83
{
84
string[] strs = ((string)value).Split(new char[]
{ ',' });
85
SimpleCustomType sct = new SimpleCustomType();
86
sct.Min = int.Parse(strs[0]);
87
sct.Max = int.Parse(strs[1]);
88
return sct;
89
}
90
return base.ConvertFrom(context, culture, value);
91
}
92
93
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
94
{
95
// 我们自定义类型的实例如何转换成字符串类型呢?看下面
96
if (destinationType == typeof(string))
97
{
98
if (value is SimpleCustomType)
99
{
100
SimpleCustomType sct = (SimpleCustomType)value;
101
return sct.Min.ToString() + ", " + sct.Max.ToString();
102
}
103
}
104
return base.ConvertTo(context, culture, value, destinationType);
105
}
106
}
107
#endregion
108
}
109
编译, 拖到windows窗体上,点击查看属性浏览器,

wow, 是灰色的,不能使用. 为啥?
..... 那是因为属性浏览器不知道如何转换我的属性,
我们不是写了类型转换器了吗? 没有被使用, ...
又要用到Attribute了,这真是个好东西呀
在上面的代码中的属性ComplexProperty 用TypeConverter (TypeConverterAttribute的缩写)指定一下我们自定义的类型的类型转换器即可.
[TypeConverter(typeof(SimpleCustomTypeConverter))]
public SimpleCustomType ComplexProperty

{

get
{ return complexField; }

set
{ complexField = value; }
}
再编译......查看属性浏览器

OK了
The end.