Exemplo n.º 1
0
        /// <summary>
        /// 从POCO实体类获取跟当前实体类的属性名称相同的属性的值,拷贝到当前实体类中,完成数据的映射。
        /// 要求拷贝的同名属性是读写属性且类型相同。
        /// </summary>
        /// <param name="pocoClass">POCO实体类,提供源数据</param>
        /// <param name="isChange">是否改变属性的修改状态</param>
        /// <returns>映射成功的属性数量</returns>
        public int MapFrom(object pocoClass, bool isChange)
        {
            if (pocoClass == null)
            {
                return(0);
            }
            int count  = 0;
            int fcount = this.PropertyNames.Length;

            INamedMemberAccessor[]            accessors = new INamedMemberAccessor[fcount];
            DelegatedReflectionMemberAccessor drm       = new DelegatedReflectionMemberAccessor();
            Type type = pocoClass.GetType();
            var  ef   = EntityFieldsCache.Item(this.GetType());

            for (int i = 0; i < fcount; i++)
            {
                //实体类的属性字段可能跟属性名称不一样 edit at 2015.2.11
                string perpertyName = ef.GetPropertyName(PropertyNames[i]);
                accessors[i] = drm.TryFindAccessor(type, perpertyName);
            }
            for (int i = 0; i < fcount; i++)
            {
                if (accessors[i] != null)
                {
                    this.PropertyValues[i] = accessors[i].GetValue(pocoClass);
                    if (isChange) //设置属性修改状态
                    {
                        this.changedlist[i] = true;
                    }
                    count++;
                }
            }
            return(count);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 将当前实体类的属性值映射到相同属性名称的POCO实体类中。要求拷贝的同名属性是读写属性且类型相同。
        /// </summary>
        /// <param name="pocoClass">POCO实体类</param>
        /// <returns>映射成功的属性数量</returns>
        public int MapToPOCO(object pocoClass)
        {
            if (pocoClass == null)
            {
                return(0);
            }
            int count  = 0;
            int fcount = this.PropertyNames.Length;

            INamedMemberAccessor[]            accessors = new INamedMemberAccessor[fcount];
            DelegatedReflectionMemberAccessor drm       = new DelegatedReflectionMemberAccessor();
            Type type = pocoClass.GetType();

            for (int i = 0; i < fcount; i++)
            {
                accessors[i] = drm.TryFindAccessor(type, PropertyNames[i]);
            }
            for (int i = 0; i < fcount; i++)
            {
                if (accessors[i] != null)
                {
                    accessors[i].SetValue(pocoClass, this.PropertyValues[i]);
                    count++;
                }
            }
            return(count);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 将当前实体类的属性值(非空值)映射到相同属性名称的POCO实体类中。要求拷贝的同名属性是读写属性且类型相同。
        /// </summary>
        /// <param name="pocoClass">POCO实体类</param>
        /// <returns>映射成功的属性数量</returns>
        public int MapToPOCO(object pocoClass)
        {
            if (pocoClass == null)
            {
                return(0);
            }
            int count  = 0;
            int fcount = this.PropertyNames.Length;

            INamedMemberAccessor[]            accessors = new INamedMemberAccessor[fcount];
            DelegatedReflectionMemberAccessor drm       = new DelegatedReflectionMemberAccessor();
            Type type = pocoClass.GetType();
            var  ef   = EntityFieldsCache.Item(this.GetType());

            for (int i = 0; i < fcount; i++)
            {
                //实体类的属性字段可能跟属性名称不一样 edit at 2015.10.24
                string perpertyName = ef.GetPropertyName(PropertyNames[i]);
                accessors[i] = drm.TryFindAccessor(type, perpertyName);
            }
            for (int i = 0; i < fcount; i++)
            {
                if (accessors[i] != null)
                {
                    //this.PropertyValues[i] 可能为空,这会导致下面的赋值报错
                    //这种情况可能发生在实体类的数据是经过OQL部分Select 字段造成的,或者字段本身的值为NULL
                    object Value = this.PropertyValues[i];
                    if (Value != null && Value != DBNull.Value)
                    {
                        accessors[i].SetValue(pocoClass, this.PropertyValues[i]);
                        count++;
                    }
                }
            }
            return(count);
        }