Пример #1
0
        public static Action<object, object, TypeMappingContext> GetComplexPropertiesMapper(
			Func<object, object> fromPropertyGetter, Action<object, object> toPropertySetter,
			MapperCollection mappers, ObjectFactory objectFactory)
        {
            return delegate(object from, object to, TypeMappingContext context)
            {
                object obj = fromPropertyGetter(from);

                if (obj != null)
                {
                    var type = obj.GetType();
                    Type targetType = null;
                    var bySourceType = mappers.GetBySourceType(type, out targetType);

                    Error.MappingException_IfMapperIsNull(bySourceType, type);

                    object toObject = objectFactory.CreateTargetObject(obj, targetType, context.MappingContext);

                    var arg = new TypeMappingContext
                    {
                        From = obj,
                        To = toObject,
                        MappingContext = context.MappingContext
                    };

                    bySourceType(obj, toObject, arg);

                    toPropertySetter(to, toObject);
                }
            };
        }
Пример #2
0
        public static Action<object, object, TypeMappingContext> GetComplexEnumerablePropertiesMapper(
			Type toEnumerableType, Func<object, object> fromEnumerableGetter,
			Action<object, object> toEnumerableSetter, ObjectFactory objectFactory, MapperCollection mappers)
        {
            return delegate(object from, object to, TypeMappingContext context)
            {
                object obj = fromEnumerableGetter(from);

                if (obj != null)
                {
                    object targetObject = objectFactory.CreateTargetObject(obj, toEnumerableType, context.MappingContext);

                    var ctx = new TypeMappingContext
                    {
                        From = obj,
                        To = targetObject,
                        MappingContext = context.MappingContext
                    };

                    Mappers.MapComplexEnumerables(ctx, mappers, objectFactory);

                    toEnumerableSetter(to, targetObject);
                }
            };
        }
Пример #3
0
        public void CreateTargetObject_TargetTypeIsCollection_ReturnsList()
        {
            //Arrange
            var objectFactory = new ObjectFactory();

            List<MainEntity> list = new List<MainEntity>
            {
                new MainEntity(),
                new MainEntity()
            };

            //Act
            object obj = objectFactory.CreateTargetObject(list, typeof(IEnumerable<MainEntity>), null);

            //Assert
            Assert.AreEqual<System.Type>(typeof(List<MainEntity>), obj.GetType());
            Assert.AreEqual<int>(0, ((List<MainEntity>)obj).Count);
        }
Пример #4
0
        public void CreateTargetObject_TargetTypeIsArray_ReturnsArray()
        {
            //Arrange
            var objectFactory = new ObjectFactory();

            List<MainEntity> list = new List<MainEntity>
            {
                new MainEntity(),
                new MainEntity()
            };

            //Act
            object obj = objectFactory.CreateTargetObject(list, typeof(MainEntity[]), null);

            //Assert
            Assert.AreEqual<System.Type>(typeof(MainEntity[]), obj.GetType());
            Assert.AreEqual<int>(list.Count, ((System.Array)obj).Length);

            foreach (object current in (System.Array)obj)
            {
                Assert.IsNull(current);
            }
        }
Пример #5
0
        public static Action<object, object, TypeMappingContext> GetMapperFromRootType(
			Type toPropertyType, Action<object, object> toPropertySetter,
			MapperCollection mappers, ObjectFactory objectFactory)
        {
            return delegate(object from, object to, TypeMappingContext context)
            {
                var action = mappers.Get(from.GetType(), toPropertyType);

                Error.MappingException_IfMapperIsNull(action, from.GetType());

                object obj = objectFactory.CreateTargetObject(from, toPropertyType, context.MappingContext);

                var arg = new TypeMappingContext
                {
                    From = from,
                    To = obj,
                    MappingContext = context.MappingContext
                };

                action(from, obj, arg);

                toPropertySetter(to, obj);
            };
        }
Пример #6
0
        public void CreateTargetObject_TargetTypeIsComplexObject_ReturnsComplexObject()
        {
            //Arrange
            var objectFactory = new ObjectFactory();

            //Act
            object obj = objectFactory.CreateTargetObject(new MainEntity(), typeof(MainEntity), null);

            //Assert
            Assert.AreEqual<Type>(typeof(MainEntity), obj.GetType());
        }
Пример #7
0
        public static void MapComplexEnumerables(TypeMappingContext context, MapperCollection mappers, ObjectFactory objectFactory)
        {
            Type fromType = context.From.GetType();
            Type toType = context.To.GetType();

            var addItemMethod = ReflectionHelper.GetAddItemMethod(toType);

            var addItemMethodInfo = ReflectionHelper.GetAddItemMethodInfo(toType);

            int num = 0;

            foreach (object current in context.From as IEnumerable)
            {
                if (current == null)
                {
                    addItemMethod(context.To, null, num, addItemMethodInfo);
                    num++;
                }
                else
                {
                    Type targetType = null;
                    Type currentType = current.GetType();
                    var bySourceType = mappers.GetBySourceType(currentType, out targetType);

                    Error.MappingException_IfMapperIsNull(bySourceType, currentType);

                    object obj = objectFactory.CreateTargetObject(current, targetType, context.MappingContext);

                    var arg = new TypeMappingContext
                    {
                        From = current,
                        To = obj,
                        MappingContext = context.MappingContext
                    };

                    bySourceType(current, obj, arg);

                    addItemMethod(context.To, obj, num, addItemMethodInfo);

                    num++;
                }
            }
        }
Пример #8
0
        public static Action<object, object, TypeMappingContext> GetSimpleEnumerablePropertiesMapper(
			PropertyInfo fromPropertyInfo, PropertyInfo toPropertyInfo, ObjectFactory objectFactory,
			ConverterCollection converters, Func<object, object> fromPropertyGetter, Action<object, object> toPropertySetter)
        {
            if (fromPropertyInfo.PropertyType == toPropertyInfo.PropertyType)
            {
                return delegate(object from, object to, TypeMappingContext ctxt)
                {
                    toPropertySetter(to, fromPropertyGetter(from));
                };
            }
            else
            {
                Type enumerableItemType = ReflectionHelper.GetEnumerableItemType(fromPropertyInfo.PropertyType);
                Type toItemType = ReflectionHelper.GetEnumerableItemType(toPropertyInfo.PropertyType);
                var addMethod = ReflectionHelper.GetAddItemMethod(toPropertyInfo.PropertyType);
                var addMethodInfo = ReflectionHelper.GetAddItemMethodInfo(toPropertyInfo.PropertyType);
                var converter = converters.Get(enumerableItemType, toItemType);

                if (converter == null)
                {
                    converter = ((object fromProperty, TypeMappingContext context) =>
                        Convert.ChangeType(fromProperty, toItemType));
                }

                return delegate(object from, object to, TypeMappingContext context)
                {
                    object obj = fromPropertyGetter(from);
                    if (obj != null)
                    {
                        object targetObject = objectFactory.CreateTargetObject(
                            obj, toPropertyInfo.PropertyType, context.MappingContext);

                        int num = 0;

                        foreach (object current in obj as IEnumerable)
                        {
                            addMethod(targetObject, converter(current, context), num, addMethodInfo);
                            num++;
                        }

                        toPropertySetter(to, targetObject);
                    }
                };
            }
        }