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); } }; }
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); } }; }
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); }; }
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++; } } }
public static Action<object, object, TypeMappingContext> GetMapperToRootType( Func<object, object> fromPropertyGetter, MapperCollection mappers) { return delegate(object from, object to, TypeMappingContext context) { object obj = fromPropertyGetter(from); if (obj != null) { var action = mappers.Get(obj.GetType(), context.To.GetType()); Error.MappingException_IfMapperIsNull(action, obj.GetType()); var arg = new TypeMappingContext { From = obj, To = to, MappingContext = context.MappingContext }; action(obj, to, arg); } }; }