复杂属性设计时支持实现

 实现代码如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
  1
  2     [TypeConverter(typeof(TableInfoConverter))]
  3    public partial class TableInfo
  4ExpandedBlockStart.gifContractedBlock.gif    {
  5ContractedSubBlock.gifExpandedSubBlockStart.gif        常量#region 常量
  6        #endregion

  7
  8ContractedSubBlock.gifExpandedSubBlockStart.gif        成员#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
 23ContractedSubBlock.gifExpandedSubBlockStart.gif        属性#region 属性
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 25        /// 数据表名称
 26        /// </summary>

 27        [XmlAttribute("Name")]
 28        [Description("数据表名称")]
 29        public virtual string Name
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 31            get
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 33                return m_Name == null ? string.Empty : m_Name;
 34            }

 35            set
 36ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 37                m_Name = value;
 38            }

 39        }

 40ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 41        /// 数据表别名
 42        /// </summary>

 43        [XmlAttribute("Alias")]
 44        [Description("数据表别名")]
 45        public virtual string Alias
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 47            get
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 49                return m_Alias == null ? string.Empty : m_Alias;
 50            }

 51            set
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 53                m_Alias = value;
 54            }

 55        }

 56ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 57        /// 主键名称
 58        /// </summary>

 59        [XmlAttribute("PrimaryKey")]
 60        [Description("主键名称")]
 61        public virtual string PrimaryKey
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 63            get
 64ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 65                return m_PrimaryKey == null ? string.Empty : m_PrimaryKey;
 66            }

 67            set
 68ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 69                m_PrimaryKey = value;
 70            }

 71        }

 72ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 73        /// 自增列名称
 74        /// </summary>

 75        [XmlAttribute("AutoIncrease")]
 76        [Description("自增列名称")]
 77        public virtual string AutoIncrease
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 79            get
 80ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 81                return m_AutoIncrease == null ? string.Empty : m_AutoIncrease;
 82            }

 83            set
 84ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 85                m_AutoIncrease = value;
 86            }

 87        }

 88ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 89        /// 数据列集合
 90        /// </summary>

 91        [XmlArray("Columns")]
 92        [Description("数据列集合")]
 93        public virtual ColumnInfoCollection Columns
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 95            get
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 97                return m_Columns;
 98
 99            }

100            set
101ExpandedSubBlockStart.gifContractedSubBlock.gif            {
102                m_Columns = value;
103            }

104        }

105        #endregion

106
107ContractedSubBlock.gifExpandedSubBlockStart.gif        方法#region 方法
108ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
109        /// 构造器
110        /// </summary>  

111        public TableInfo()
112ExpandedSubBlockStart.gifContractedSubBlock.gif        { }
113
114
115        #endregion

116    }

117 

  

ContractedBlock.gif ExpandedBlockStart.gif Code
  1
  2    public class TableInfoConverter : ExpandableObjectConverter
  3ExpandedBlockStart.gifContractedBlock.gif    {
  4        // 返回值能否将String类型转换为Address类型
  5        //sourceType表示要转换的类型
  6        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  7ExpandedSubBlockStart.gifContractedSubBlock.gif        {
  8            if (sourceType == typeof(string))
  9ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 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)
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 19            if (destinationType == typeof(string))
 20ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 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)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 31            if (value == null)
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 33                return new TableInfo();
 34            }

 35
 36            if (value is string)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 38                string s = (string)value;
 39                if (s.Length == 0)
 40ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 41                    return new TableInfo();
 42                }

 43
 44                string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
 45
 46                if (parts.Length != 4)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 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)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 72            if (value != null)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 74                if (!(value is TableInfo))
 75ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 76                    throw new ArgumentException(
 77                        "Invalid TableInfo""value");
 78                }

 79            }

 80
 81            if (destinationType == typeof(string))
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 83                if (value == null)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 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,
 92ExpandedSubBlockStart.gifContractedSubBlock.gif                    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

转载于:https://www.cnblogs.com/NRabbit/archive/2009/02/09/1736195.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值