先定义要展示的属性:
1using System;
2using System.ComponentModel;
3
4namespace WroxControls
5{
6 /// <summary>
7 /// Dog 的摘要说明。
8 /// </summary>
9 ///
10 public enum eBreed
11 { Dalmation,Labrador,GoldenRetrieve,Mutt,BlackLabradorRetriever
12 }
13
14 [TypeConverter(typeof(DogConverter))]
15 public struct Dog
16 {
17 private string _Name;
18 private eBreed _Breed;
19 private int _Age;
20
21 public Dog(string name,eBreed breed,int age)
22 {
23 _Name = name;
24 _Breed = breed;
25 _Age = age;
26 }
27
28 public string Name
29 {
30 get { return _Name; }
31 set { _Name = value;}
32 }
33 public eBreed Breed
34 {
35 get { return _Breed; }
36 set { _Breed = value;}
37 }
38 public int Age
39 {
40 get { return _Age; }
41 set { _Age = value;}
42 }
43
44 }
45}
46
2using System.ComponentModel;
3
4namespace WroxControls
5{
6 /// <summary>
7 /// Dog 的摘要说明。
8 /// </summary>
9 ///
10 public enum eBreed
11 { Dalmation,Labrador,GoldenRetrieve,Mutt,BlackLabradorRetriever
12 }
13
14 [TypeConverter(typeof(DogConverter))]
15 public struct Dog
16 {
17 private string _Name;
18 private eBreed _Breed;
19 private int _Age;
20
21 public Dog(string name,eBreed breed,int age)
22 {
23 _Name = name;
24 _Breed = breed;
25 _Age = age;
26 }
27
28 public string Name
29 {
30 get { return _Name; }
31 set { _Name = value;}
32 }
33 public eBreed Breed
34 {
35 get { return _Breed; }
36 set { _Breed = value;}
37 }
38 public int Age
39 {
40 get { return _Age; }
41 set { _Age = value;}
42 }
43
44 }
45}
46
再定义转换函数:
1using System;
2using System.Reflection;
3using System.Web;
4using System.Web.UI.WebControls;
5using System.ComponentModel;
6using System.ComponentModel.Design.Serialization;
7
8namespace WroxControls
9{
10 /// <summary>
11 /// DogConverter 的摘要说明。
12 /// </summary>
13 public class DogConverter:ExpandableObjectConverter
14 {
15 public DogConverter()
16 {
17 //
18 // TODO: 在此处添加构造函数逻辑
19 //
20 }
21 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
22 {
23 if( sourceType == typeof(string) )
24 return true;
25 else
26 return base.CanConvertFrom (context, sourceType);
27 }
28 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
29 {
30 if(destinationType == typeof(InstanceDescriptor) || destinationType == typeof(string))
31 return true;
32 else
33 return base.CanConvertTo (context, destinationType);
34 }
35 public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
36 {
37 string sValue = value as string;
38 if(sValue != null)
39 {
40 string[] v = sValue.Split(new char[]{','});
41 return new Dog(v[0],(eBreed)Enum.Parse(typeof(eBreed),v[1]),Int32.Parse(v[2]));
42 }
43 else
44 return base.ConvertFrom (context, culture, value);
45 }
46 public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
47 {
48 Dog dg = (Dog)value;
49 if(destinationType == typeof(InstanceDescriptor))
50 {
51 Type[] parms = new Type[]{ typeof(string),typeof(eBreed),typeof(int)};
52 object[] vals = new object[]{dg.Name,dg.Breed,dg.Age};
53 return new InstanceDescriptor(typeof(Dog).GetConstructor(parms),vals);
54
55 }
56 else if( destinationType == typeof(string))
57 {
58 return string.Format("{0},{1},{2}",dg.Name,dg.Breed,dg.Age);
59 }
60 return base.ConvertTo (context, culture, value, destinationType);
61 }
62
63 }
64}
65
最后定义控件并引用:2using System.Reflection;
3using System.Web;
4using System.Web.UI.WebControls;
5using System.ComponentModel;
6using System.ComponentModel.Design.Serialization;
7
8namespace WroxControls
9{
10 /// <summary>
11 /// DogConverter 的摘要说明。
12 /// </summary>
13 public class DogConverter:ExpandableObjectConverter
14 {
15 public DogConverter()
16 {
17 //
18 // TODO: 在此处添加构造函数逻辑
19 //
20 }
21 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
22 {
23 if( sourceType == typeof(string) )
24 return true;
25 else
26 return base.CanConvertFrom (context, sourceType);
27 }
28 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
29 {
30 if(destinationType == typeof(InstanceDescriptor) || destinationType == typeof(string))
31 return true;
32 else
33 return base.CanConvertTo (context, destinationType);
34 }
35 public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
36 {
37 string sValue = value as string;
38 if(sValue != null)
39 {
40 string[] v = sValue.Split(new char[]{','});
41 return new Dog(v[0],(eBreed)Enum.Parse(typeof(eBreed),v[1]),Int32.Parse(v[2]));
42 }
43 else
44 return base.ConvertFrom (context, culture, value);
45 }
46 public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
47 {
48 Dog dg = (Dog)value;
49 if(destinationType == typeof(InstanceDescriptor))
50 {
51 Type[] parms = new Type[]{ typeof(string),typeof(eBreed),typeof(int)};
52 object[] vals = new object[]{dg.Name,dg.Breed,dg.Age};
53 return new InstanceDescriptor(typeof(Dog).GetConstructor(parms),vals);
54
55 }
56 else if( destinationType == typeof(string))
57 {
58 return string.Format("{0},{1},{2}",dg.Name,dg.Breed,dg.Age);
59 }
60 return base.ConvertTo (context, culture, value, destinationType);
61 }
62
63 }
64}
65
1using System;
2using System.Web;
3using System.Web.UI;
4using System.ComponentModel;
5
6namespace WroxControls
7{
8 /// <summary>
9 /// DogControl 的摘要说明。
10 /// </summary>
11 public class DogControl:System.Web.UI.Control
12 {
13 public DogControl()
14 {
15 //
16 // TODO: 在此处添加构造函数逻辑
17 //
18 }
19 private Dog _DogAttribute;
20 public Dog DogAttribute
21 {
22 get
23 {
24 return _DogAttribute;
25 }
26 set
27 {
28 _DogAttribute = value;
29 }
30 }
31 }
32}
2using System.Web;
3using System.Web.UI;
4using System.ComponentModel;
5
6namespace WroxControls
7{
8 /// <summary>
9 /// DogControl 的摘要说明。
10 /// </summary>
11 public class DogControl:System.Web.UI.Control
12 {
13 public DogControl()
14 {
15 //
16 // TODO: 在此处添加构造函数逻辑
17 //
18 }
19 private Dog _DogAttribute;
20 public Dog DogAttribute
21 {
22 get
23 {
24 return _DogAttribute;
25 }
26 set
27 {
28 _DogAttribute = value;
29 }
30 }
31 }
32}