复杂属性设计时支持实现
2009-02-09 16:38 Kevin-wang 阅读(207) 评论(0) 收藏 举报实现代码如下:

 Code
Code1

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

 
     {
{5

 常量#region 常量
        常量#region 常量6
 #endregion
        #endregion7

8

 成员#region 成员
        成员#region 成员9
 //  数据表名称
        //  数据表名称 10
 private string m_Name = string.Empty;
        private string m_Name = string.Empty;11
 //  数据表别名
        //  数据表别名 12
 private string m_Alias = string.Empty;
        private string m_Alias = string.Empty;13
 //  主键名称
        //  主键名称 14
 private string m_PrimaryKey = string.Empty;
        private string m_PrimaryKey = string.Empty;15
 //  自增列名称
        //  自增列名称 16
 private string m_AutoIncrease = string.Empty;
        private string m_AutoIncrease = string.Empty;17
 //  数据列集合
        //  数据列集合 18
 private ColumnInfoCollection m_Columns = new ColumnInfoCollection();
        private ColumnInfoCollection m_Columns = new ColumnInfoCollection();19

20

21
 #endregion
        #endregion22

23

 属性#region 属性
        属性#region 属性24

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

 
         {
{31
 get
            get32

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

 
             {
{37
 m_Name = value;
                m_Name = value;38
 }
            }39
 }
        }40

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

 
         {
{47
 get
            get48

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

 
             {
{53
 m_Alias = value;
                m_Alias = value;54
 }
            }55
 }
        }56

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

 
         {
{63
 get
            get64

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

 
             {
{69
 m_PrimaryKey = value;
                m_PrimaryKey = value;70
 }
            }71
 }
        }72

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

 
         {
{79
 get
            get80

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

 
             {
{85
 m_AutoIncrease = value;
                m_AutoIncrease = value;86
 }
            }87
 }
        }88

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

 
         {
{95
 get
            get96

 
             {
{97
 return m_Columns;
                return m_Columns;98

99
 }
            }100
 set
            set101

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

107

 方法#region 方法
        方法#region 方法108

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

 
         { }
{ }113

114

115
 #endregion
        #endregion116
 }
    }117
 
 

 Code
Code1

2
 public class TableInfoConverter : ExpandableObjectConverter
    public class TableInfoConverter : ExpandableObjectConverter3

 
     {
{4
 // 返回值能否将String类型转换为Address类型
        // 返回值能否将String类型转换为Address类型5
 //sourceType表示要转换的类型
        //sourceType表示要转换的类型6
 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)7

 
         {
{8
 if (sourceType == typeof(string))
            if (sourceType == typeof(string))9

 
             {
{10
 return true;
                return true;11
 }
            }12
 return base.CanConvertFrom(context, sourceType);
            return base.CanConvertFrom(context, sourceType);13
 }
        }14

15
 // 返回值能否将Address类型转换为String类型
        // 返回值能否将Address类型转换为String类型16
 //sourceType表示要转换到的类型
        //sourceType表示要转换到的类型17
 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)18

 
         {
{19
 if (destinationType == typeof(string))
            if (destinationType == typeof(string))20

 
             {
{21
 return true;
                return true;22
 }
            }23
 return base.CanConvertTo(context, destinationType);
            return base.CanConvertTo(context, destinationType);24
 }
        }25

26
 //将String类型转换为Address类型
        //将String类型转换为Address类型27
 //value为要转换的类型
        //value为要转换的类型28
 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture,
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture,29
 object value)
            object value)30

 
         {
{31
 if (value == null)
            if (value == null)32

 
             {
{33
 return new TableInfo();
                return new TableInfo();34
 }
            }35

36
 if (value is string)
            if (value is string)37

 
             {
{38
 string s = (string)value;
                string s = (string)value;39
 if (s.Length == 0)
                if (s.Length == 0)40

 
                 {
{41
 return new TableInfo();
                    return new TableInfo();42
 }
                }43

44
 string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
                string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);45

46
 if (parts.Length != 4)
                if (parts.Length != 4)47

 
                 {
{48
 throw new ArgumentException("Invalid TableInfo", "value");
                    throw new ArgumentException("Invalid TableInfo", "value");49
 }
                }50
 //返回指定类型转换器
                //返回指定类型转换器51
 TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
                TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));52
 _Info = new DSKJ.XCode.DAL.TableInfo();
                _Info = new DSKJ.XCode.DAL.TableInfo();53

54
 _Info.Name=(string)stringConverter.ConvertFromString(context, culture, parts[0]);
                _Info.Name=(string)stringConverter.ConvertFromString(context, culture, parts[0]);55
 _Info.Alias=(string)stringConverter.ConvertFromString(context, culture, parts[1]);
                _Info.Alias=(string)stringConverter.ConvertFromString(context, culture, parts[1]);56
 _Info.PrimaryKey=(string)stringConverter.ConvertFromString(context, culture, parts[2]);
                _Info.PrimaryKey=(string)stringConverter.ConvertFromString(context, culture, parts[2]);57
 _Info.AutoIncrease = (string)stringConverter.ConvertFromString(context, culture, parts[3]);
                _Info.AutoIncrease = (string)stringConverter.ConvertFromString(context, culture, parts[3]);58
 return _Info;
                return _Info;59

60
 }
            }61

62

63
 return base.ConvertFrom(context, culture, value);
            return base.ConvertFrom(context, culture, value);64
 }
        }65

66
 //将Address类型转换为String类型
        //将Address类型转换为String类型67
 //value为要转换的类型
        //value为要转换的类型68
 public override object ConvertTo(
        public override object ConvertTo(69
 ITypeDescriptorContext context,
            ITypeDescriptorContext context,70
 CultureInfo culture, object value, Type destinationType)
            CultureInfo culture, object value, Type destinationType)71

 
         {
{72
 if (value != null)
            if (value != null)73

 
             {
{74
 if (!(value is TableInfo))
                if (!(value is TableInfo))75

 
                 {
{76
 throw new ArgumentException(
                    throw new ArgumentException(77
 "Invalid TableInfo", "value");
                        "Invalid TableInfo", "value");78
 }
                }79
 }
            }80

81
 if (destinationType == typeof(string))
            if (destinationType == typeof(string))82

 
             {
{83
 if (value == null)
                if (value == null)84

 
                 {
{85
 return String.Empty;
                    return String.Empty;86
 }
                }87

88
 TableInfo _TableInfo = (TableInfo)value;
                TableInfo _TableInfo = (TableInfo)value;89

90
 TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
                TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));91
 return String.Join(culture.TextInfo.ListSeparator,
                return String.Join(culture.TextInfo.ListSeparator,92

 new string[]
                    new string[]  {
{93
 stringConverter.ConvertToString(context, culture, _TableInfo.Name),
                                     stringConverter.ConvertToString(context, culture, _TableInfo.Name),94
 stringConverter.ConvertToString(context, culture, _TableInfo.Alias),
                                     stringConverter.ConvertToString(context, culture, _TableInfo.Alias),95
 stringConverter.ConvertToString(context, culture, _TableInfo.PrimaryKey),
                                     stringConverter.ConvertToString(context, culture, _TableInfo.PrimaryKey),96
 stringConverter.ConvertToString(context, culture, _TableInfo.AutoIncrease)
                                     stringConverter.ConvertToString(context, culture, _TableInfo.AutoIncrease)97
 });
                                 });98
 }
            }99
 return base.ConvertTo(context, culture, value,
            return base.ConvertTo(context, culture, value,100
 destinationType);
                destinationType);101
 }
        }102

103
 }
    }104

 
                    
                     
                    
                 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号