示例#1
0
        /// <summary>
        /// 根据属性名称获得属性值,设置值的时候,检查类型
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <returns>属性值</returns>
        public object this[string name]
        {
            get
            {
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException();
                }
                else if (this._GetItemSchema(name) == null)
                {
                    throw new ArgumentOutOfRangeException(string.Format("属性\"{0}\"不存在", name));
                }
                else if (!this.Table.ContainsKey(name))
                {
                    return(null);
                }
                else
                {
                    return(this.Table[name]);
                }
            }
            set
            {
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException();
                }
                NamedItemSchema item = this._GetItemSchema(name);
                if (item == null)
                {
                    throw new ArgumentOutOfRangeException(string.Format("属性\"{0}\"不存在", name));
                }

                // 检查类型是否匹配
                object convertedValue = null;
                if (!Utility.Convert(value, item.RealType, out convertedValue))
                {
                    // 转换失败,抛出异常
                    throw new InvalidCastException(string.Format("数据\"{0}\"无法转化为类型\"{1}\"",
                                                                 value,
                                                                 item.DataType.ToString(),
                                                                 item.RealType));
                }

                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException();
                }
                else if (!this.Table.ContainsKey(name))
                {
                    throw new ArgumentOutOfRangeException(string.Format("属性\"{0}\"不存在", name));
                }
                else
                {
                    this.Table[name] = convertedValue;
                }
            }
        }
        /// <summary>
        /// 根据项名称获得项显示名称
        /// </summary>
        /// <param name="itemName">项名称</param>
        /// <returns>显示名称</returns>
        public string GetDisplayName(string itemName)
        {
            NamedItemSchema item = (NamedItemSchema)(object)this.GetItem(itemName);

            if (item == null)
            {
                throw new ArgumentOutOfRangeException();
            }
            return(item.DisplayName);
        }
        /// <summary>
        /// 根据项名称获得项类型
        /// </summary>
        /// <param name="itemName">项名称</param>
        /// <returns>项类型</returns>
        public BizDataType GetDataType(string itemName)
        {
            NamedItemSchema item = (NamedItemSchema)(object)this.GetItem(itemName);

            if (item == null)
            {
                throw new ArgumentOutOfRangeException();
            }
            return(item.DataType);
        }
        /// <summary>
        /// 更新一个项模式
        /// </summary>
        /// <param name="oldItemName">项的旧名称</param>
        /// <param name="newItemSchema">新的项模式</param>
        /// <returns>如果项不存在,或者存在重名的话,则返回false,否则返回true</returns>
        public bool Update(string oldItemName, T newItemSchema)
        {
            NamedItemSchema i = (NamedItemSchema)(object)newItemSchema;

            if (newItemSchema == null || string.IsNullOrEmpty(i.Name) ||
                // 修改了名称,并且新名称已经存在
                (string.Compare(oldItemName, i.Name, true) != 0 && this.Table.ContainsKey(i.Name.ToLower())))
            {
                return(false);
            }
            this.Remove(oldItemName);
            this.Add(newItemSchema);
            return(true);
        }
        /// <summary>
        /// 获得所有数据表支持的项
        /// </summary>
        /// <returns>所有数据表支持的项</returns>
        public T[] GetDataTableSupportedItems()
        {
            T[]      items = this.Items;
            List <T> list  = new List <T>();

            if (items != null)
            {
                foreach (T item in items)
                {
                    NamedItemSchema i = (NamedItemSchema)(object)item;
                    list.Add(item);
                }
            }
            return(list.ToArray());
        }
        /// <summary>
        /// 添加一个项模式
        /// </summary>
        /// <param name="item">项模式</param>
        /// <returns>如果项模式的名称合法,并且名称不重复,则返回true,否则返回false</returns>
        public virtual bool Add(T item)
        {
            NamedItemSchema i = (NamedItemSchema)(object)item;

            if (item == null || string.IsNullOrEmpty(i.Name))
            {
                return(false);
            }
            else if (this.Table.ContainsKey(i.Name.ToLower()))
            {
                return(false);
            }
            else
            {
                this.Table.Add(i.Name.ToLower(), item);
                this.Names.Add(i.Name);

                this._Items = null;

                return(true);
            }
        }