Exemple #1
0
        /// <summary>
        /// 取得属性的值
        /// </summary>
        /// <typeparam name="T">属性值类型</typeparam>
        /// <param name="propName">属性名称</param>
        /// <returns>对象属性值</returns>
        public T GetValue <T>(NamedProperty <T> propName)
        {
            if (propName == null)
            {
                throw new ArgumentNullException("propName", "参数不能为空");
            }

            if (this.Properties.ContainsKey(propName.Name))
            {
                object obj = this.Properties[propName.Name];

                if (obj is DBNull)
                {
                    return(propName.DefaultValue);
                }
                else
                {
                    return((T)Convert.ChangeType(this.Properties[propName.Name], typeof(T)));
                }
            }
            else
            {
                return(propName.DefaultValue);
            }
        }
Exemple #2
0
        /// <summary>
        /// 设置一个属性的值
        /// </summary>
        /// <typeparam name="T">属性值类型</typeparam>
        /// <param name="propName">属性名称</param>
        /// <param name="propValue">属性值</param>
        public void SetValue <T>(NamedProperty <T> propName, T propValue)
        {
            if (propName == null)
            {
                throw new ArgumentNullException("propName", "参数不能为空");
            }

            if (!this.Properties.ContainsKey(propName.Name))
            {
                this.Properties.Add(propName.Name, propValue == null ? propName.DefaultValue : propValue);
            }
            else
            {
                this.Properties[propName.Name] = propValue == null ? propName.DefaultValue : propValue;
            }
        }