复杂属性设计时支持实现
2009-02-09 16:38 Kevin-wang 阅读(202) 评论(0) 编辑 收藏 举报实现代码如下:
Code
1
2 [TypeConverter(typeof(TableInfoConverter))]
3 public partial class TableInfo
4 {
5 常量#region 常量
6 #endregion
7
8 成员#region 成员
9 // 数据表名称
10 private string m_Name = string.Empty;
11 // 数据表别名
12 private string m_Alias = string.Empty;
13 // 主键名称
14 private string m_PrimaryKey = string.Empty;
15 // 自增列名称
16 private string m_AutoIncrease = string.Empty;
17 // 数据列集合
18 private ColumnInfoCollection m_Columns = new ColumnInfoCollection();
19
20
21 #endregion
22
23 属性#region 属性
24 /**//// <summary>
25 /// 数据表名称
26 /// </summary>
27 [XmlAttribute("Name")]
28 [Description("数据表名称")]
29 public virtual string Name
30 {
31 get
32 {
33 return m_Name == null ? string.Empty : m_Name;
34 }
35 set
36 {
37 m_Name = value;
38 }
39 }
40 /**//// <summary>
41 /// 数据表别名
42 /// </summary>
43 [XmlAttribute("Alias")]
44 [Description("数据表别名")]
45 public virtual string Alias
46 {
47 get
48 {
49 return m_Alias == null ? string.Empty : m_Alias;
50 }
51 set
52 {
53 m_Alias = value;
54 }
55 }
56 /**//// <summary>
57 /// 主键名称
58 /// </summary>
59 [XmlAttribute("PrimaryKey")]
60 [Description("主键名称")]
61 public virtual string PrimaryKey
62 {
63 get
64 {
65 return m_PrimaryKey == null ? string.Empty : m_PrimaryKey;
66 }
67 set
68 {
69 m_PrimaryKey = value;
70 }
71 }
72 /**//// <summary>
73 /// 自增列名称
74 /// </summary>
75 [XmlAttribute("AutoIncrease")]
76 [Description("自增列名称")]
77 public virtual string AutoIncrease
78 {
79 get
80 {
81 return m_AutoIncrease == null ? string.Empty : m_AutoIncrease;
82 }
83 set
84 {
85 m_AutoIncrease = value;
86 }
87 }
88 /**//// <summary>
89 /// 数据列集合
90 /// </summary>
91 [XmlArray("Columns")]
92 [Description("数据列集合")]
93 public virtual ColumnInfoCollection Columns
94 {
95 get
96 {
97 return m_Columns;
98
99 }
100 set
101 {
102 m_Columns = value;
103 }
104 }
105 #endregion
106
107 方法#region 方法
108 /**//// <summary>
109 /// 构造器
110 /// </summary>
111 public TableInfo()
112 { }
113
114
115 #endregion
116 }
117
1
2 [TypeConverter(typeof(TableInfoConverter))]
3 public partial class TableInfo
4 {
5 常量#region 常量
6 #endregion
7
8 成员#region 成员
9 // 数据表名称
10 private string m_Name = string.Empty;
11 // 数据表别名
12 private string m_Alias = string.Empty;
13 // 主键名称
14 private string m_PrimaryKey = string.Empty;
15 // 自增列名称
16 private string m_AutoIncrease = string.Empty;
17 // 数据列集合
18 private ColumnInfoCollection m_Columns = new ColumnInfoCollection();
19
20
21 #endregion
22
23 属性#region 属性
24 /**//// <summary>
25 /// 数据表名称
26 /// </summary>
27 [XmlAttribute("Name")]
28 [Description("数据表名称")]
29 public virtual string Name
30 {
31 get
32 {
33 return m_Name == null ? string.Empty : m_Name;
34 }
35 set
36 {
37 m_Name = value;
38 }
39 }
40 /**//// <summary>
41 /// 数据表别名
42 /// </summary>
43 [XmlAttribute("Alias")]
44 [Description("数据表别名")]
45 public virtual string Alias
46 {
47 get
48 {
49 return m_Alias == null ? string.Empty : m_Alias;
50 }
51 set
52 {
53 m_Alias = value;
54 }
55 }
56 /**//// <summary>
57 /// 主键名称
58 /// </summary>
59 [XmlAttribute("PrimaryKey")]
60 [Description("主键名称")]
61 public virtual string PrimaryKey
62 {
63 get
64 {
65 return m_PrimaryKey == null ? string.Empty : m_PrimaryKey;
66 }
67 set
68 {
69 m_PrimaryKey = value;
70 }
71 }
72 /**//// <summary>
73 /// 自增列名称
74 /// </summary>
75 [XmlAttribute("AutoIncrease")]
76 [Description("自增列名称")]
77 public virtual string AutoIncrease
78 {
79 get
80 {
81 return m_AutoIncrease == null ? string.Empty : m_AutoIncrease;
82 }
83 set
84 {
85 m_AutoIncrease = value;
86 }
87 }
88 /**//// <summary>
89 /// 数据列集合
90 /// </summary>
91 [XmlArray("Columns")]
92 [Description("数据列集合")]
93 public virtual ColumnInfoCollection Columns
94 {
95 get
96 {
97 return m_Columns;
98
99 }
100 set
101 {
102 m_Columns = value;
103 }
104 }
105 #endregion
106
107 方法#region 方法
108 /**//// <summary>
109 /// 构造器
110 /// </summary>
111 public TableInfo()
112 { }
113
114
115 #endregion
116 }
117
Code
1
2 public class TableInfoConverter : ExpandableObjectConverter
3 {
4 // 返回值能否将String类型转换为Address类型
5 //sourceType表示要转换的类型
6 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
7 {
8 if (sourceType == typeof(string))
9 {
10 return true;
11 }
12 return base.CanConvertFrom(context, sourceType);
13 }
14
15 // 返回值能否将Address类型转换为String类型
16 //sourceType表示要转换到的类型
17 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
18 {
19 if (destinationType == typeof(string))
20 {
21 return true;
22 }
23 return base.CanConvertTo(context, destinationType);
24 }
25
26 //将String类型转换为Address类型
27 //value为要转换的类型
28 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture,
29 object value)
30 {
31 if (value == null)
32 {
33 return new TableInfo();
34 }
35
36 if (value is string)
37 {
38 string s = (string)value;
39 if (s.Length == 0)
40 {
41 return new TableInfo();
42 }
43
44 string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
45
46 if (parts.Length != 4)
47 {
48 throw new ArgumentException("Invalid TableInfo", "value");
49 }
50 //返回指定类型转换器
51 TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
52 _Info = new DSKJ.XCode.DAL.TableInfo();
53
54 _Info.Name=(string)stringConverter.ConvertFromString(context, culture, parts[0]);
55 _Info.Alias=(string)stringConverter.ConvertFromString(context, culture, parts[1]);
56 _Info.PrimaryKey=(string)stringConverter.ConvertFromString(context, culture, parts[2]);
57 _Info.AutoIncrease = (string)stringConverter.ConvertFromString(context, culture, parts[3]);
58 return _Info;
59
60 }
61
62
63 return base.ConvertFrom(context, culture, value);
64 }
65
66 //将Address类型转换为String类型
67 //value为要转换的类型
68 public override object ConvertTo(
69 ITypeDescriptorContext context,
70 CultureInfo culture, object value, Type destinationType)
71 {
72 if (value != null)
73 {
74 if (!(value is TableInfo))
75 {
76 throw new ArgumentException(
77 "Invalid TableInfo", "value");
78 }
79 }
80
81 if (destinationType == typeof(string))
82 {
83 if (value == null)
84 {
85 return String.Empty;
86 }
87
88 TableInfo _TableInfo = (TableInfo)value;
89
90 TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
91 return String.Join(culture.TextInfo.ListSeparator,
92 new string[] {
93 stringConverter.ConvertToString(context, culture, _TableInfo.Name),
94 stringConverter.ConvertToString(context, culture, _TableInfo.Alias),
95 stringConverter.ConvertToString(context, culture, _TableInfo.PrimaryKey),
96 stringConverter.ConvertToString(context, culture, _TableInfo.AutoIncrease)
97 });
98 }
99 return base.ConvertTo(context, culture, value,
100 destinationType);
101 }
102
103 }
104
1
2 public class TableInfoConverter : ExpandableObjectConverter
3 {
4 // 返回值能否将String类型转换为Address类型
5 //sourceType表示要转换的类型
6 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
7 {
8 if (sourceType == typeof(string))
9 {
10 return true;
11 }
12 return base.CanConvertFrom(context, sourceType);
13 }
14
15 // 返回值能否将Address类型转换为String类型
16 //sourceType表示要转换到的类型
17 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
18 {
19 if (destinationType == typeof(string))
20 {
21 return true;
22 }
23 return base.CanConvertTo(context, destinationType);
24 }
25
26 //将String类型转换为Address类型
27 //value为要转换的类型
28 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture,
29 object value)
30 {
31 if (value == null)
32 {
33 return new TableInfo();
34 }
35
36 if (value is string)
37 {
38 string s = (string)value;
39 if (s.Length == 0)
40 {
41 return new TableInfo();
42 }
43
44 string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
45
46 if (parts.Length != 4)
47 {
48 throw new ArgumentException("Invalid TableInfo", "value");
49 }
50 //返回指定类型转换器
51 TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
52 _Info = new DSKJ.XCode.DAL.TableInfo();
53
54 _Info.Name=(string)stringConverter.ConvertFromString(context, culture, parts[0]);
55 _Info.Alias=(string)stringConverter.ConvertFromString(context, culture, parts[1]);
56 _Info.PrimaryKey=(string)stringConverter.ConvertFromString(context, culture, parts[2]);
57 _Info.AutoIncrease = (string)stringConverter.ConvertFromString(context, culture, parts[3]);
58 return _Info;
59
60 }
61
62
63 return base.ConvertFrom(context, culture, value);
64 }
65
66 //将Address类型转换为String类型
67 //value为要转换的类型
68 public override object ConvertTo(
69 ITypeDescriptorContext context,
70 CultureInfo culture, object value, Type destinationType)
71 {
72 if (value != null)
73 {
74 if (!(value is TableInfo))
75 {
76 throw new ArgumentException(
77 "Invalid TableInfo", "value");
78 }
79 }
80
81 if (destinationType == typeof(string))
82 {
83 if (value == null)
84 {
85 return String.Empty;
86 }
87
88 TableInfo _TableInfo = (TableInfo)value;
89
90 TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
91 return String.Join(culture.TextInfo.ListSeparator,
92 new string[] {
93 stringConverter.ConvertToString(context, culture, _TableInfo.Name),
94 stringConverter.ConvertToString(context, culture, _TableInfo.Alias),
95 stringConverter.ConvertToString(context, culture, _TableInfo.PrimaryKey),
96 stringConverter.ConvertToString(context, culture, _TableInfo.AutoIncrease)
97 });
98 }
99 return base.ConvertTo(context, culture, value,
100 destinationType);
101 }
102
103 }
104