Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public virtual IGenericDto SetEntity(IEntity value)
        {
            Type typeValue = value.GetType();
            Dictionary <string, PropertyInfo> entityDictionary = null;
            Dictionary <string, PropertyInfo> dtoDictionary    = null;

            string classEntityName = typeValue.Namespace + "." + typeValue.Name;
            string classDtoName    = this.GetType().Namespace + "." + this.GetType().Name;

            if (DtoConfiguration.objectpropertySet.ContainsKey(classEntityName))
            {
                entityDictionary = DtoConfiguration.objectpropertySet[classEntityName];
            }
            else
            {
                entityDictionary = DtoConfiguration.setTypes(typeof(T));
            }

            if (DtoConfiguration.objectpropertySet.ContainsKey(classDtoName))
            {
                dtoDictionary = DtoConfiguration.objectpropertySet[classDtoName];
            }
            else
            {
                dtoDictionary = DtoConfiguration.setTypes(this.GetType());
            }

            setObjectTo(dtoDictionary, entityDictionary, value);

            return(this);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private T findEntity()
        {
            T entity = new T();

            Dictionary <string, PropertyInfo> entityDictionary = null;
            Dictionary <string, PropertyInfo> dtoDictionary    = null;

            string classDtoName    = this.GetType().Namespace + "." + this.GetType().Name;
            string classEntityName = entity.GetType().Namespace + "." + entity.GetType().Name;

            if (DtoConfiguration.objectpropertySet.ContainsKey(classEntityName))
            {
                entityDictionary = DtoConfiguration.objectpropertySet[classEntityName];
            }
            else
            {
                entityDictionary = DtoConfiguration.setTypes(typeof(T));
            }

            if (DtoConfiguration.objectpropertySet.ContainsKey(classDtoName))
            {
                dtoDictionary = DtoConfiguration.objectpropertySet[classDtoName];
            }
            else
            {
                dtoDictionary = DtoConfiguration.setTypes(this.GetType());
            }

            foreach (string key in dtoDictionary.Keys)
            {
                if (entityDictionary.ContainsKey(key))
                {
                    if (dtoDictionary[key].GetValue(this) is IList)
                    {
                        IGenericDto obj = null;
                        try
                        {
                            obj = ClassUtils.getNewInstance(ClassUtils.getElementType(dtoDictionary[key].PropertyType)) as IGenericDto;
                        }
                        catch { }

                        if (obj != null)
                        {
                            var r = obj.GetEntity((IEnumerable <IGenericDto>)dtoDictionary[key].GetValue(this));
                            var l = ClassUtils.getNewInstance(entityDictionary[key].PropertyType);

                            foreach (var item in r)
                            {
                                ((IList)l).Add(item);
                            }

                            entityDictionary[key].SetValue(entity, l);
                        }
                        else
                        {
                            if (dtoDictionary[key].PropertyType.GetTypeInfo().BaseType.Name == "Array")
                            {
                                var val = obj.GetEntity((IEnumerable <IGenericDto>)dtoDictionary[key].GetValue(this));
                                if (val is IPclCloneable)
                                {
                                    entityDictionary[key].SetValue(entity, ((IPclCloneable)val).Clone());
                                }
                            }
                            else
                            {
                                var l   = ClassUtils.getNewInstance(entityDictionary[key].PropertyType);
                                var val = dtoDictionary[key].GetValue(this);
                                foreach (var item in (IList)val)
                                {
                                    if (item is IGenericDto)
                                    {
                                        ((IList)l).Add(((IGenericDto)item).GetEntity());
                                    }
                                    else if (item is IPclCloneable)
                                    {
                                        ((IList)l).Add(((IPclCloneable)item).Clone());
                                    }
                                }

                                entityDictionary[key].SetValue(entity, l);
                            }
                        }
                    }
                    else
                    if (dtoDictionary[key].GetValue(this) is IGenericDto)
                    {
                        var l   = ClassUtils.getNewInstance(entityDictionary[key].PropertyType);
                        var val = ((IGenericDto)dtoDictionary[key].GetValue(this)).GetEntity();
                        entityDictionary[key].SetValue(entity, val);
                    }
                    else if (((dtoDictionary[key].GetValue(this) is IEnumerable) == false || (dtoDictionary[key].GetValue(this) is System.String)) &&
                             (dtoDictionary[key].GetValue(this) is IGenericDto) == false)
                    {
                        entityDictionary[key].SetValue(entity, dtoDictionary[key].GetValue(this));
                    }
                }
            }

            return(entity);
        }