Exemplo n.º 1
0
        /// <summary>
        /// 获取属性的值,如果没有,将返回属性类型的默认值;注意该方法调用将发起属性获取事件 。
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public object GetPropertyValueWithEvent(string propertyName)
        {
            EntityFields ef        = EntityFieldsCache.Item(this.GetType());
            string       fieldName = ef.GetPropertyField(propertyName);

            if (fieldName != null)
            {
                this.OnPropertyGeting(fieldName);
                object result = PropertyList(fieldName);
                if (result == null || result == DBNull.Value)
                {
                    Type propType = ef.GetPropertyType(fieldName);
                    result = Activator.CreateInstance(propType);//活动类型的默认值
                }
                return(result);
            }
            return(new ArgumentOutOfRangeException("不存在指定的属性名:" + propertyName));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取或者设置指定属性名称的值,属性名必须是一个PDF.NET实体类的属性(调用了getProperty 和 setProperty方法),不能是普通属性。
        /// 如果属性不存在,获取该属性值将为null,而设置该属性值将抛出异常。
        /// </summary>
        /// <param name="propertyName">属性名称</param>
        /// <returns></returns>
        public object this[string propertyName]
        {
            get
            {
                EntityFields ef        = EntityFieldsCache.Item(this.GetType());
                string       fieldName = ef.GetPropertyField(propertyName);
                if (fieldName != null)
                {
                    this.OnPropertyGeting(fieldName);
                    return(PropertyList(fieldName));
                }
                //获取虚拟的字段值
                return(PropertyList(propertyName));;
            }
            set
            {
                EntityFields ef        = EntityFieldsCache.Item(this.GetType());
                string       fieldName = ef.GetPropertyField(propertyName);

                if (fieldName != null)
                {
                    //如果是实体类基础定义的字段,必须检查设置的值得类型
                    //2017.5.5 增加类型相容转换处理,包括空字符串,可用于大批量文本数据导入情况
                    Type fieldType = ef.GetPropertyType(fieldName);
                    try
                    {
                        object Value = CommonUtil.ChangeType(value, fieldType);
                        this.setProperty(fieldName, Value);
                    }
                    catch (Exception ex)
                    {
                        throw new ArgumentException("实体类的属性字段" + propertyName + " 需要"
                                                    + fieldType.Name + " 类型的值,但准备赋予的值不是该类型!", ex);
                    }
                }
                else
                {
                    //设置虚拟的字段值
                    this.setProperty(propertyName, value);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取或者设置指定属性名称的值,属性名必须是一个PDF.NET实体类的属性(调用了getProperty 和 setProperty方法),不能是普通属性。
        /// 如果属性不存在,获取该属性值将为null,而设置该属性值将抛出异常。
        /// </summary>
        /// <param name="propertyName">属性名称</param>
        /// <returns></returns>
        public object this[string propertyName]
        {
            get
            {
                EntityFields ef        = EntityFieldsCache.Item(this.GetType());
                string       fieldName = ef.GetPropertyField(propertyName);
                if (fieldName != null)
                {
                    this.OnPropertyGeting(fieldName);
                    return(PropertyList(fieldName));
                }
                //获取虚拟的字段值
                return(PropertyList(propertyName));;
            }
            set
            {
                EntityFields ef        = EntityFieldsCache.Item(this.GetType());
                string       fieldName = ef.GetPropertyField(propertyName);

                if (fieldName != null)
                {
                    //如果是实体类基础定义的字段,必须检查设置的值得类型
                    if (value != null)
                    {
                        Type fieldType = ef.GetPropertyType(fieldName);
                        if (value.GetType() != fieldType)
                        {
                            throw new ArgumentException("实体类的属性字段" + propertyName + " 需要"
                                                        + fieldType.Name + " 类型的值,但准备赋予的值不是该类型!");
                        }
                    }
                    this.setProperty(fieldName, value);
                }
                else
                {
                    //设置虚拟的字段值
                    this.setProperty(propertyName, value);
                }
            }
        }