Esempio n. 1
0
        /// <summary>
        /// 对象属性拷贝(同名字段)
        /// </summary>
        /// <param name="source">源对象</param>
        /// <param name="target">目标对象</param>
        /// <returns></returns>
        public static int ObjectCopy(object source, object target)
        {
            if (source == null || target == null)
            {
                return(0);
            }
            int  ret        = 0;
            Type sourceType = source.GetType();
            Type targetType = source.GetType();

            ClassInfoHandle sourceInfo = ClassInfoManager.GetClassHandle(sourceType);
            ClassInfoHandle targetInfo = ClassInfoManager.GetClassHandle(targetType);

            foreach (FieldInfoHandle fInfo in sourceInfo.FieldInfo)
            {
                FieldInfoHandle tInfo = targetInfo.FieldInfo[fInfo.FieldName];
                if (tInfo != null)
                {
                    if (tInfo.FieldType == fInfo.FieldType)
                    {
                        tInfo.SetValue(target, fInfo.GetValue(source));
                        ret++;
                    }
                }
            }


            return(ret);
        }
Esempio n. 2
0
        private static List <FieldInfoHandle> GetFieldInfos(Type objType)
        {
            string key = objType.FullName;
            List <FieldInfoHandle> ret = null;



            if (!_dicFieldInfo.TryGetValue(key, out ret))
            {
                ret = new List <FieldInfoHandle>();
                _dicFieldInfo[key] = ret;

                Type currentType = objType;

                while (currentType != null)
                {
                    FieldInfo[] fInfos = currentType.GetFields(FastValueGetSet.AllBindingFlags);
                    foreach (FieldInfo finfo in fInfos)
                    {
                        GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(finfo);
                        SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(finfo);
                        FieldInfoHandle     handle    = new FieldInfoHandle(objType, getHandle, setHandle, finfo.FieldType, finfo.Name, finfo);
                        ret.Add(handle);
                    }
                    currentType = currentType.BaseType;
                    if (currentType == typeof(object))
                    {
                        break;
                    }
                }
            }

            return(ret);
        }
Esempio n. 3
0
        /// <summary>
        /// 初始化类型的属性信息
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns>如果已经初始化过侧返回false</returns>
        private static void InitClassPropertyInfos(Type type)
        {
            string fullName = type.FullName;

            //实例化本类型的句柄
            CreateInstanceHandler createrHandel = FastValueGetSet.GetCreateInstanceHandlerWithOutCache(type);
            Dictionary <string, PropertyInfoHandle> dicPropertys = new Dictionary <string, PropertyInfoHandle>();
            Dictionary <string, FieldInfoHandle>    dicField     = new Dictionary <string, FieldInfoHandle>();

            //属性信息句柄
            PropertyInfo[] destproper = type.GetProperties(FastValueGetSet.AllBindingFlags);
            FieldInfo[]    allField   = type.GetFields(FastValueGetSet.AllBindingFlags);
            //int index = 0;
            ///读取属性别名
            foreach (PropertyInfo pinf in destproper)
            {
                ///通过属性来反射
                string proName = pinf.Name;

                FastPropertyHandler getHandle = FastValueGetSet.GetGetMethodInfo(proName, type);
                FastPropertyHandler setHandle = FastValueGetSet.GetSetMethodInfo(proName, type);
                if (getHandle != null || setHandle != null)
                {
                    PropertyInfoHandle classProperty = new PropertyInfoHandle(type, getHandle, setHandle, pinf.PropertyType, pinf.Name);
                    dicPropertys.Add(pinf.Name, classProperty);
                }
            }

            ///读取属性别名
            foreach (FieldInfo fInf in allField)
            {
                string proName = fInf.Name;

                GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(fInf);
                SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(fInf);
                if (getHandle != null || setHandle != null)
                {
                    FieldInfoHandle fieldInfo = new FieldInfoHandle(type, getHandle, setHandle, fInf.FieldType, fInf.Name, fInf);
                    dicField.Add(fInf.Name, fieldInfo);
                }
            }


            ClassInfoHandle classInfo = new ClassInfoHandle(type, createrHandel, dicPropertys, dicField);

            dicClass.Add(fullName, classInfo);
        }
Esempio n. 4
0
        /// <summary>
        /// 初始化类型的属性信息
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns>如果已经初始化过侧返回false</returns>
        private static EntityInfoHandle InitEntityPropertyInfos(Type type,
                                                                Dictionary <string, EntityConfigInfo> dicConfigs)
        {
            if (type == null)
            {
                return(null);
            }


            string                fullName      = type.FullName;
            TableAttribute        tableAtt      = new TableAttribute();
            CreateInstanceHandler createrHandle = null;

            //实例化本类型的句柄
            if (!type.IsGenericType)
            {
                createrHandle = FastValueGetSet.GetCreateInstanceHandlerWithOutCache(type);
            }
            Dictionary <string, EntityPropertyInfo> dicPropertys = new Dictionary <string, EntityPropertyInfo>();
            Dictionary <string, EntityMappingInfo>  dicMapping   = new Dictionary <string, EntityMappingInfo>();

            Dictionary <string, EntityParam>            dicParamsInfo   = new Dictionary <string, EntityParam>();
            Dictionary <string, TableRelationAttribute> dicRelationInfo = new Dictionary <string, TableRelationAttribute>();

            FillEntityInfos(dicParamsInfo, dicRelationInfo, type, tableAtt, dicConfigs);
            DBInfo           db        = DataAccessLoader.GetDBInfo(tableAtt.BelongDB);
            IDBAdapter       idb       = db.CurrentDbAdapter;
            EntityInfoHandle classInfo = new EntityInfoHandle(type, createrHandle, tableAtt, db);

            Dictionary <string, bool> dicNotFoundParam    = new Dictionary <string, bool>();
            Dictionary <string, bool> dicNotFoundRelation = new Dictionary <string, bool>();

            FillNotFoundField(dicParamsInfo, dicRelationInfo, dicNotFoundParam, dicNotFoundRelation);

            //属性信息句柄
            List <FieldInfoHandle> lstFields = FieldInfoHandle.GetFieldInfos(type, FastValueGetSet.AllBindingFlags, true);
            DataBaseOperate        oper      = db.DefaultOperate;

            ///读取属性别名
            foreach (FieldInfoHandle finf in lstFields)
            {
                ///通过属性来反射
                EntityParam ep = null;


                if (dicParamsInfo.TryGetValue(finf.FieldName, out ep))
                {
                    //if (tableAtt.IsParamNameUpper)
                    //{
                    //    ep.ParamName = ep.ParamName.ToUpper();
                    //}
                    string proName = ep.PropertyName;
                    //GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(finf);
                    //SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(finf);
                    if (finf.HasGetHandle || finf.HasSetHandle)
                    {
                        PropertyInfo       pinfo          = type.GetProperty(ep.PropertyName, FastValueGetSet.AllBindingFlags);
                        EntityPropertyInfo entityProperty = new EntityPropertyInfo(
                            classInfo, finf.GetHandle, finf.SetHandle, ep, finf.FieldType, finf.FieldName,
                            finf.BelongFieldInfo, pinfo);
                        dicPropertys.Add(proName, entityProperty);
                        dicNotFoundParam.Remove(finf.FieldName);
                    }
                }
                else
                {
                    TableRelationAttribute tableMappingAtt = null;

                    if (dicRelationInfo.TryGetValue(finf.FieldName, out tableMappingAtt))
                    {
                        Type targetType = DefaultType.GetRealValueType(finf.FieldType);
                        tableMappingAtt.SetEntity(type, targetType);
                        //GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(finf);
                        //SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(finf);
                        PropertyInfo      pinfo             = type.GetProperty(tableMappingAtt.PropertyName, FastValueGetSet.AllBindingFlags);
                        EntityMappingInfo entityMappingInfo = new EntityMappingInfo(
                            type, finf.GetHandle, finf.SetHandle, tableMappingAtt,
                            finf.FieldName, finf.FieldType, finf.BelongFieldInfo, pinfo);
                        dicMapping.Add(tableMappingAtt.PropertyName, entityMappingInfo);
                        dicNotFoundRelation.Remove(finf.FieldName);
                    }
                }
            }



            if (dicNotFoundParam.Count > 0 || dicNotFoundRelation.Count > 0)
            {
                StringBuilder message = new StringBuilder();

                foreach (KeyValuePair <string, bool> kvp in dicNotFoundParam)
                {
                    message.Append(kvp.Key + "、");
                }


                foreach (KeyValuePair <string, bool> kvp in dicNotFoundRelation)
                {
                    message.Append(kvp.Key + "、");
                }
                if (message.Length > 0)
                {
                    message.Remove(message.Length - 1, 1);
                }
                message.Insert(0, "类:" + type.FullName + " 找不到字段");
                throw new MissingFieldException(message.ToString());
            }
            classInfo.SetInfoHandles(dicPropertys, dicMapping);
            FillAttributeInfo(type, classInfo);
            _dicClass[fullName] = classInfo;
            return(classInfo);
        }