实现代码如下:
1

2
[TypeConverter(typeof(TableInfoConverter))]3
public partial class TableInfo4

{5

常量#region 常量6
#endregion7

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
#endregion22

23

属性#region 属性24

/**//// <summary>25
/// 数据表名称26
/// </summary>27
[XmlAttribute("Name")]28
[Description("数据表名称")]29
public virtual string Name30

{31
get32

{33
return m_Name == null ? string.Empty : m_Name;34
}35
set36

{37
m_Name = value;38
}39
}40

/**//// <summary>41
/// 数据表别名42
/// </summary>43
[XmlAttribute("Alias")]44
[Description("数据表别名")]45
public virtual string Alias46

{47
get48

{49
return m_Alias == null ? string.Empty : m_Alias;50
}51
set52

{53
m_Alias = value;54
}55
}56

/**//// <summary>57
/// 主键名称58
/// </summary>59
[XmlAttribute("PrimaryKey")]60
[Description("主键名称")]61
public virtual string PrimaryKey62

{63
get64

{65
return m_PrimaryKey == null ? string.Empty : m_PrimaryKey;66
}67
set68

{69
m_PrimaryKey = value;70
}71
}72

/**//// <summary>73
/// 自增列名称74
/// </summary>75
[XmlAttribute("AutoIncrease")]76
[Description("自增列名称")]77
public virtual string AutoIncrease78

{79
get80

{81
return m_AutoIncrease == null ? string.Empty : m_AutoIncrease;82
}83
set84

{85
m_AutoIncrease = value;86
}87
}88

/**//// <summary>89
/// 数据列集合90
/// </summary>91
[XmlArray("Columns")]92
[Description("数据列集合")]93
public virtual ColumnInfoCollection Columns94

{95
get96

{97
return m_Columns;98

99
}100
set101

{102
m_Columns = value;103
}104
}105
#endregion106

107

方法#region 方法108

/**//// <summary>109
/// 构造器110
/// </summary> 111
public TableInfo()112

{ }113

114

115
#endregion116
}117
1

2
public class TableInfoConverter : ExpandableObjectConverter3

{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


浙公网安备 33010602011771号