Exemplo n.º 1
0
        private static void MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper,
                                             object mappedObject, PropertyMap propertyMap)
        {
            if (!propertyMap.CanResolveValue())
            {
                return;
            }

            var result     = propertyMap.ResolveValue(context);
            var newContext = context.CreateMemberContext(null, result.Value, null, result.Type, propertyMap);

            if (!propertyMap.ShouldAssignValue(newContext))
            {
                return;
            }

            try
            {
                var propertyValueToAssign = mapper.Map(newContext);

                if (propertyMap.CanBeSet)
                {
                    propertyMap.DestinationProperty.SetValue(mappedObject, propertyValueToAssign);
                }
            }
            catch (MapperMappingException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new MapperMappingException(newContext, ex);
            }
        }
Exemplo n.º 2
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var sourceDelegateType = context.SourceType.GetGenericArguments()[0];
            var destDelegateType = context.DestinationType.GetGenericArguments()[0];
            var expression = (LambdaExpression) context.SourceValue;

            if (sourceDelegateType.GetGenericTypeDefinition() != destDelegateType.GetGenericTypeDefinition())
                throw new AutoMapperMappingException("Source and destination expressions must be of the same type.");

            var parameters = expression.Parameters.ToArray();
            var body = expression.Body;

            for (int i = 0; i < expression.Parameters.Count; i++)
            {
                var sourceParamType = sourceDelegateType.GetGenericArguments()[i];
                var destParamType = destDelegateType.GetGenericArguments()[i];

                if (sourceParamType == destParamType)
                    continue;

                var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(destParamType, sourceParamType);

                if (typeMap == null)
                    throw new AutoMapperMappingException(
                        $"Could not find type map from destination type {destParamType} to source type {sourceParamType}. Use CreateMap to create a map from the source to destination types.");

                var oldParam = expression.Parameters[i];
                var newParam = Expression.Parameter(typeMap.SourceType, oldParam.Name);
                parameters[i] = newParam;
                var visitor = new MappingVisitor(typeMap, oldParam, newParam);
                body = visitor.Visit(body);
            }
            return Expression.Lambda(body, parameters);
        }
Exemplo n.º 3
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (IsDataReader(context))
            {
                var dataReader = (IDataReader)context.SourceValue;

                var destinationElementType = TypeHelper.GetElementType(context.DestinationType);
                var buildFrom = CreateBuilder(destinationElementType, dataReader);

                var results = ObjectCreator.CreateList(destinationElementType);
                while (dataReader.Read())
                {
                    results.Add(buildFrom(dataReader));
                }

                return results;
            }

            if (IsDataRecord(context))
            {
                var dataRecord = context.SourceValue as IDataRecord;
                var buildFrom = CreateBuilder(context.DestinationType, dataRecord);

                var result = buildFrom(dataRecord);
                MapPropertyValues(context, mapper, result);

                return result;
            }

            return null;
        }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var sourceElementType = TypeHelper.GetElementType(context.SourceValue.GetType());
            var destinationElementType = TypeHelper.GetElementType(context.DestinationValue.GetType());
            var equivilencyExpression = GetEquivilentExpression(context);

            var sourceEnumerable = context.SourceValue as IEnumerable;
            var destEnumerable = (IEnumerable)context.DestinationValue;

            var destItems = destEnumerable.Cast<object>().ToList();
            var sourceItems = sourceEnumerable.Cast<object>().ToList();
            var compareSourceToDestination = sourceItems.ToDictionary(s => s, s => destItems.FirstOrDefault(d => equivilencyExpression.IsEquivlent(s, d)));

            var actualDestType = destEnumerable.GetType();

            var addMethod = actualDestType.GetMethod("Add");
            foreach (var keypair in compareSourceToDestination)
            {
                if (keypair.Value == null)
                    addMethod.Invoke(destEnumerable, new[] { Mapper.Map(keypair.Key, sourceElementType, destinationElementType) });
                else
                    Mapper.Map(keypair.Key, keypair.Value, sourceElementType, destinationElementType);
            }

            var removeMethod = actualDestType.GetMethod("Remove");
            foreach (var removedItem in destItems.Except(compareSourceToDestination.Values))
                removeMethod.Invoke(destEnumerable, new[] { removedItem });

            return destEnumerable;
        }
Exemplo n.º 5
0
            protected override object GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
            {
                var result = context.DestinationValue;

                context.SetResolvedDestinationValue(result);
                return(result);
            }
Exemplo n.º 6
0
        public object ResolveValue(ResolutionContext context, IMappingEngineRunner mappingEngine)
        {
            var ctorArgs = new List<object>();

            foreach (var map in CtorParams)
            {
                var result = map.ResolveValue(context);

                var sourceType = result.Type;
                var destinationType = map.Parameter.ParameterType;

                var typeMap = mappingEngine.ConfigurationProvider.ResolveTypeMap(result, destinationType);

                Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceType;

                var newContext = context.CreateTypeContext(typeMap, result.Value, null, targetSourceType,
                    destinationType);

                if (typeMap == null && map.Parameter.IsOptional)
                {
                    object value = map.Parameter.DefaultValue;
                    ctorArgs.Add(value);
                }
                else
                {
                    var value = mappingEngine.Map(newContext);
                    ctorArgs.Add(value);
                }
            }

            return _runtimeCtor.Value(ctorArgs.ToArray());
        }
Exemplo n.º 7
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (IsDataReader(context))
            {
                var useYieldReturn         = ((IConfiguration)mapper.ConfigurationProvider).DataReaderMapperYieldReturnEnabled;
                var destinationElementType = TypeHelper.GetElementType(context.DestinationType);
                var results = MapDataReaderToEnumerable(context, mapper, destinationElementType, useYieldReturn);

                if (useYieldReturn)
                {
                    var adapterBuilder = GetDelegateToCreateEnumerableAdapter(destinationElementType);
                    return(adapterBuilder(results));
                }

                return(results);
            }

            if (IsDataRecord(context))
            {
                var dataRecord = context.SourceValue as IDataRecord;
                var buildFrom  = CreateBuilder(context.DestinationType, dataRecord);
                var result     = buildFrom(dataRecord);
                MapPropertyValues(context, mapper, result);
                return(result);
            }

            return(null);
        }
Exemplo n.º 8
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var sourceDelegateType = context.SourceType.GetGenericArguments()[0];
            var destDelegateType   = context.DestinationType.GetGenericArguments()[0];
            var expression         = (LambdaExpression)context.SourceValue;

            if (sourceDelegateType.GetGenericTypeDefinition() != destDelegateType.GetGenericTypeDefinition())
            {
                throw new AutoMapperMappingException("Source and destination expressions must be of the same type.");
            }

            var destArgType = destDelegateType.GetGenericArguments()[0];

            if (destArgType.IsGenericType())
            {
                destArgType = destArgType.GetGenericArguments()[0];
            }
            var sourceArgType = sourceDelegateType.GetGenericArguments()[0];

            if (sourceArgType.IsGenericType())
            {
                sourceArgType = sourceArgType.GetGenericArguments()[0];
            }

            var typeMap = Mapper.FindTypeMapFor(destArgType, sourceArgType);

            var parentMasterVisitor = new MappingVisitor(destDelegateType.GetGenericArguments());
            var typeMapVisitor      = new MappingVisitor(typeMap, expression.Parameters[0], Expression.Parameter(destDelegateType.GetGenericArguments()[0], expression.Parameters[0].Name), parentMasterVisitor, destDelegateType.GetGenericArguments());

            // Map expression body and variable seperately
            var parameters = expression.Parameters.Select(typeMapVisitor.Visit).OfType <ParameterExpression>();
            var body       = typeMapVisitor.Visit(expression.Body);

            return(Expression.Lambda(body, parameters));
        }
Exemplo n.º 9
0
        public object ResolveValue(ResolutionContext context, IMappingEngineRunner mappingEngine)
        {
            var ctorArgs = new List <object>();

            foreach (var map in CtorParams)
            {
                var result = map.ResolveValue(context);

                var sourceType      = result.Type;
                var destinationType = map.Parameter.ParameterType;

                var typeMap = mappingEngine.ConfigurationProvider.ResolveTypeMap(result, destinationType);

                Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceType;

                var newContext = context.CreateTypeContext(typeMap, result.Value, null, targetSourceType,
                                                           destinationType);

                if (typeMap == null && map.Parameter.IsOptional)
                {
                    object value = map.Parameter.DefaultValue;
                    ctorArgs.Add(value);
                }
                else
                {
                    var value = mappingEngine.Map(newContext);
                    ctorArgs.Add(value);
                }
            }

            return(_runtimeCtor.Value(ctorArgs.ToArray()));
        }
Exemplo n.º 10
0
        static IEnumerable MapDataReaderToEnumerable(ResolutionContext context, IMappingEngineRunner mapper, Type destinationElementType, bool useYieldReturn)
        {
            var dataReader          = (IDataReader)context.SourceValue;
            var resolveUsingContext = context;

            if (context.TypeMap == null)
            {
                var configurationProvider = mapper.ConfigurationProvider;

                // v3.3.1
                //TypeMap typeMap = configurationProvider.FindTypeMapFor(context.SourceValue, context.DestinationValue, context.SourceType, destinationElementType); // replace using new

                // v4.1.1
                var typeMap = configurationProvider.FindTypeMapFor(context.SourceType, destinationElementType);
                resolveUsingContext = new ResolutionContext(typeMap, context.SourceValue, context.SourceType, destinationElementType, new MappingOperationOptions(), (IMappingEngine)mapper);
            }

            var buildFrom = CreateBuilder(destinationElementType, dataReader);

            if (useYieldReturn)
            {
                return(LoadDataReaderViaYieldReturn(dataReader, mapper, buildFrom, resolveUsingContext));
            }

            return(LoadDataReaderViaList(dataReader, mapper, buildFrom, resolveUsingContext, destinationElementType));
        }
Exemplo n.º 11
0
 private static void MapPropertyValues(ResolutionContext context, IMappingEngineRunner mapper, object result)
 {
     foreach (var propertyMap in context.TypeMap.GetPropertyMaps())
     {
         MapPropertyValue(context, mapper, result, propertyMap);
     }
 }
Exemplo n.º 12
0
            public object Map(ResolutionContext context, IMappingEngineRunner mapper)
            {
                var newSource           = context.TypeMap.Substitution(context.SourceValue);
                var substitutionContext = context.CreateValueContext(newSource, newSource.GetType());

                return(mapper.Map(substitutionContext));
            }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var destExpressArgType = context.DestinationType.GetSinglePredicateExpressionArgumentType();
            var toSourceExpression = EquivilentExpressions.GetEquivilentExpression(context.SourceType, destExpressArgType) as IToSingleSourceEquivalentExpression;

            return(toSourceExpression.ToSingleSourceExpression(context.SourceValue));
        }
Exemplo n.º 14
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            bool toEnum              = false;
            Type enumSourceType      = TypeHelper.GetEnumerationType(context.SourceType);
            Type enumDestinationType = TypeHelper.GetEnumerationType(context.DestinationType);

            if (EnumToStringMapping(context, ref toEnum))
            {
                if (context.SourceValue == null)
                {
                    return(mapper.CreateObject(context));
                }

                if (toEnum)
                {
                    var stringValue = context.SourceValue.ToString();
                    if (string.IsNullOrEmpty(stringValue))
                    {
                        return(mapper.CreateObject(context));
                    }

                    return(Enum.Parse(enumDestinationType, stringValue, true));
                }
                return(Enum.GetName(enumSourceType, context.SourceValue));
            }
            if (EnumToEnumMapping(context))
            {
                if (context.SourceValue == null)
                {
                    return(mapper.CreateObject(context));
                }

                if (!Enum.IsDefined(enumSourceType, context.SourceValue))
                {
                    return(Enum.ToObject(enumDestinationType, context.SourceValue));
                }

#if !SILVERLIGHT
                if (!Enum.GetNames(enumDestinationType).Contains(context.SourceValue.ToString()))
                {
                    Type underlyingSourceType  = Enum.GetUnderlyingType(enumSourceType);
                    var  underlyingSourceValue = Convert.ChangeType(context.SourceValue, underlyingSourceType);

                    return(Enum.ToObject(context.DestinationType, underlyingSourceValue));
                }
#endif

                return(Enum.Parse(enumDestinationType, Enum.GetName(enumSourceType, context.SourceValue), true));
            }
            if (EnumToUnderlyingTypeMapping(context, ref toEnum))
            {
                if (toEnum)
                {
                    return(Enum.Parse(enumDestinationType, context.SourceValue.ToString(), true));
                }
                return(Convert.ChangeType(context.SourceValue, context.DestinationType, null));
            }
            return(null);
        }
Exemplo n.º 15
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            bool toEnum = false;
            Type enumSourceType = TypeHelper.GetEnumerationType(context.SourceType);
            Type enumDestinationType = TypeHelper.GetEnumerationType(context.DestinationType);

            if (EnumToStringMapping(context, ref toEnum))
            {
                if (context.SourceValue == null)
                {
                    return mapper.CreateObject(context);
                }

                if (toEnum)
                {
                    var stringValue = context.SourceValue.ToString();
                    if (string.IsNullOrEmpty(stringValue))
                    {
                        return mapper.CreateObject(context);
                    }

                    return Enum.Parse(enumDestinationType, stringValue, true);
                }
                return Enum.GetName(enumSourceType, context.SourceValue);
            }
            if (EnumToEnumMapping(context))
            {
                if (context.SourceValue == null)
                {
                    return mapper.CreateObject(context);
                }

                if (!Enum.IsDefined(enumSourceType, context.SourceValue))
                {
                    return Enum.ToObject(enumDestinationType, context.SourceValue);
                }

            #if !SILVERLIGHT && !__ANDROID__
                if (!Enum.GetNames(enumDestinationType).Contains(context.SourceValue.ToString()))
                {
                    Type underlyingSourceType = Enum.GetUnderlyingType(enumSourceType);
                    var underlyingSourceValue = Convert.ChangeType(context.SourceValue, underlyingSourceType);

                    return Enum.ToObject(context.DestinationType, underlyingSourceValue);
                }
            #endif

                return Enum.Parse(enumDestinationType, Enum.GetName(enumSourceType, context.SourceValue), true);
            }
            if (EnumToUnderlyingTypeMapping(context, ref toEnum))
            {
                if (toEnum)
                {
                    return Enum.Parse(enumDestinationType, context.SourceValue.ToString(), true);
                }
                return Convert.ChangeType(context.SourceValue, context.DestinationType, null);
            }
            return null;
        }
Exemplo n.º 16
0
 public object Map(ResolutionContext context, IMappingEngineRunner mapper)
 {
     if (context.SourceValue == null)
     {
         return(mapper.FormatValue(context.CreateValueContext(null)));
     }
     return(mapper.FormatValue(context));
 }
Exemplo n.º 17
0
		public object Map(ResolutionContext context, IMappingEngineRunner mapper)
		{
			if (context.SourceValue == null)
			{
				return mapper.FormatValue(context.CreateValueContext(null));
			}
			return mapper.FormatValue(context);
		}
Exemplo n.º 18
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var mapperToUse = _mappers.First(objectMapper => objectMapper.IsMatch(context, mapper));
            object mappedObject = mapperToUse.Map(context, mapper);

            context.TypeMap.AfterMap(context.SourceValue, mappedObject);
            return mappedObject;
        }
            private void MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, object mappedObject, PropertyMap propertyMap)
            {
                if (propertyMap.CanResolveValue())
                {
                    ResolutionResult result;

                    try
                    {
                        result = propertyMap.ResolveValue(context);
                    }
                    catch (AutoMapperMappingException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        var errorContext = CreateErrorContext(context, propertyMap, null);
                        throw new AutoMapperMappingException(errorContext, ex);
                    }

                    if (result.ShouldIgnore)
                    {
                        return;
                    }

                    object destinationValue = propertyMap.DestinationProperty.GetValue(mappedObject);

                    var sourceType      = result.Type;
                    var destinationType = propertyMap.DestinationProperty.MemberType;

                    var typeMap = mapper.ConfigurationProvider.FindTypeMapFor(result, destinationType);

                    Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceType;

                    var newContext = context.CreateMemberContext(typeMap, result.Value, destinationValue, targetSourceType,
                                                                 propertyMap);

                    if (!propertyMap.ShouldAssignValue(newContext))
                    {
                        return;
                    }

                    try
                    {
                        object propertyValueToAssign = mapper.Map(newContext);

                        AssignValue(propertyMap, mappedObject, propertyValueToAssign);
                    }
                    catch (AutoMapperMappingException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        throw new AutoMapperMappingException(newContext, ex);
                    }
                }
            }
Exemplo n.º 20
0
		public object Map(ResolutionContext context, IMappingEngineRunner mapper)
		{
			if (context.SourceValue == null)
			{
				return mapper.CreateObject(context);
			}
			Func<object> converter = GetConverter(context);
			return converter != null ? converter() : null;
		}
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var typeMap = mapper.ConfigurationProvider.FindClosedGenericTypeMapFor(context);

            var newContext = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType,
                context.DestinationType);

            return mapper.Map(newContext);
        }
Exemplo n.º 22
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null)
            {
                return mapper.CreateObject(context);
            }

            return context.SourceValue;
        }
Exemplo n.º 23
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null && !mapper.ShouldMapSourceCollectionAsNull(context))
            {
                return(mapper.CreateObject(context));
            }

            return(context.SourceValue);
        }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null && !mapper.ShouldMapSourceCollectionAsNull(context))
            {
                return mapper.CreateObject(context);
            }

            return context.SourceValue;
        }
 public object Map(ResolutionContext context, IMappingEngineRunner mapper)
 {
     var source = context.SourceValue;
     var sourceType = source.GetType();
     var sourceTypeDetails = new TypeDetails(sourceType, _ => true, _ => true);
     var membersDictionary = sourceTypeDetails.PublicReadAccessors.ToDictionary(p => p.Name, p => p.GetMemberValue(source));
     var newContext = context.CreateTypeContext(null, membersDictionary, context.DestinationValue, membersDictionary.GetType(), context.DestinationType);
     return mapper.Map(newContext);
 }
Exemplo n.º 26
0
 static IEnumerable LoadDataReaderViaYieldReturn(IDataReader dataReader, IMappingEngineRunner mapper, Build buildFrom, ResolutionContext resolveUsingContext)
 {
     while (dataReader.Read())
     {
         var result = buildFrom(dataReader);
         MapPropertyValues(resolveUsingContext, mapper, result);
         yield return(result);
     }
 }
Exemplo n.º 27
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var typeMap = mapper.ConfigurationProvider.FindClosedGenericTypeMapFor(context);

            var newContext = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType,
                                                       context.DestinationType);

            return(mapper.Map(newContext));
        }
            public object Map(ResolutionContext context, IMappingEngineRunner mapper)
            {
                var newSource = context.TypeMap.Substitution(context.SourceValue);
                var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(newSource.GetType(), context.DestinationType);

                var substitutionContext = context.CreateTypeContext(typeMap, newSource, context.DestinationValue,
                    newSource.GetType(), context.DestinationType);

                return mapper.Map(substitutionContext);
            }
            public object Map(ResolutionContext context, IMappingEngineRunner mapper)
            {
                var newSource = context.TypeMap.Substitution(context.SourceValue);
                var typeMap   = mapper.ConfigurationProvider.ResolveTypeMap(newSource.GetType(), context.DestinationType);

                var substitutionContext = context.CreateTypeContext(typeMap, newSource, context.DestinationValue,
                                                                    newSource.GetType(), context.DestinationType);

                return(mapper.Map(substitutionContext));
            }
            protected override object GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
            {
                var result = mapper.CreateObject(context);

                if (result == null)
                {
                    throw new InvalidOperationException("Cannot create destination object. " + context);
                }
                return(result);
            }
Exemplo n.º 31
0
        private IConfigurationProvider GetConfigurationProvider()
        {
            IMappingEngineRunner runner = engine as IMappingEngineRunner;

            if (runner == null)
            {
                throw new ArgumentException(Resources.Error.AutoMapperInvalidEngine);
            }
            return(runner.ConfigurationProvider);
        }
Exemplo n.º 32
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            context.TypeMap.Seal();

            var    mapperToUse  = _mappers.First(objectMapper => objectMapper.IsMatch(context, mapper));
            object mappedObject = mapperToUse.Map(context, mapper);

            context.TypeMap.AfterMap(context.SourceValue, mappedObject);
            return(mappedObject);
        }
Exemplo n.º 33
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null)
            {
                return(mapper.CreateObject(context));
            }
            Func <object> converter = GetConverter(context);

            return(converter != null?converter() : null);
        }
Exemplo n.º 34
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null)
            {
                return mapper.CreateObject(context);
            }

            TypeConverter typeConverter = GetTypeConverter(context);
            return typeConverter.ConvertTo(context.SourceValue, context.DestinationType);
        }
Exemplo n.º 35
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var source            = context.SourceValue;
            var sourceType        = source.GetType();
            var sourceTypeDetails = new TypeDetails(sourceType, _ => true, _ => true);
            var membersDictionary = sourceTypeDetails.PublicReadAccessors.ToDictionary(p => p.Name, p => p.GetMemberValue(source));
            var newContext        = context.CreateTypeContext(null, membersDictionary, context.DestinationValue, membersDictionary.GetType(), context.DestinationType);

            return(mapper.Map(newContext));
        }
Exemplo n.º 36
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.SourceValue == null)
            {
                return(mapper.CreateObject(context));
            }

            TypeConverter typeConverter = GetTypeConverter(context);

            return(typeConverter.ConvertTo(context.SourceValue, context.DestinationType));
        }
Exemplo n.º 37
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var enumDestType = TypeHelper.GetEnumerationType(context.DestinationType);

            if (context.SourceValue == null)
            {
                return mapper.CreateObject(context);
            }

            return Enum.Parse(enumDestType, context.SourceValue.ToString(), true);
        }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var contextTypePair = new TypePair(context.SourceType, context.DestinationType);
            Func<TypePair, IObjectMapper> missFunc = tp => context.Engine.ConfigurationProvider.GetMappers().FirstOrDefault(m => m.IsMatch(context));
            var typeMap = mapper.ConfigurationProvider.CreateTypeMap(context.SourceType, context.DestinationType, _profileName);

            context = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType, context.DestinationType);

            var map = (context.Engine as MappingEngine)._objectMapperCache.GetOrAdd(contextTypePair, missFunc);
            return map.Map(context, mapper);
        }
Exemplo n.º 39
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (mapper == null) throw new ArgumentNullException("mapper");

            if (context.SourceValue == null)
            {
                return mapper.FormatValue(context.CreateValueContext(null));
            }
            return mapper.FormatValue(context);
        }
Exemplo n.º 40
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            context.TypeMap.Seal();

            var mapperToUse = _mappers.First(objectMapper => objectMapper.IsMatch(context, mapper));

            // check whether the context passes conditions before attempting to map the value (depth check)
            var mappedObject = !context.TypeMap.ShouldAssignValue(context) ? null : mapperToUse.Map(context, mapper);

            return mappedObject;
        }
Exemplo n.º 41
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var enumDestType = TypeHelper.GetEnumerationType(context.DestinationType);

            if (context.SourceValue == null)
            {
                return(mapper.CreateObject(context));
            }

            return(Enum.Parse(enumDestType, context.SourceValue.ToString(), true));
        }
Exemplo n.º 42
0
        protected virtual object CreateDestinationObject(ResolutionContext context, Type destinationElementType,
                                                         int count, IMappingEngineRunner mapper)
        {
            var destinationType = context.DestinationType;

            if (!destinationType.IsInterface() && !destinationType.IsArray)
            {
                return(mapper.CreateObject(context));
            }
            return(CreateDestinationObjectBase(destinationElementType, count));
        }
Exemplo n.º 43
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            context.TypeMap.Seal();

            var mapperToUse = _mappers.First(objectMapper => objectMapper.IsMatch(context, mapper));

            // check whether the context passes conditions before attempting to map the value (depth check)
            object mappedObject = !context.TypeMap.ShouldAssignValue(context) ? null : mapperToUse.Map(context, mapper);

            return(mappedObject);
        }
Exemplo n.º 44
0
        private static void MapPropertyValues(ResolutionContext context, IMappingEngineRunner mapper, object result)
        {
            if (context.TypeMap == null)
            {
                throw new MapperMappingException(context, "Missing type map configuration or unsupported mapping.");
            }

            foreach (var propertyMap in context.TypeMap.GetPropertyMaps())
            {
                MapPropertyValue(context, mapper, result, propertyMap);
            }
        }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var contextTypePair = new TypePair(context.SourceType, context.DestinationType);
            Func <TypePair, IObjectMapper> missFunc = tp => context.Engine.ConfigurationProvider.GetMappers().FirstOrDefault(m => m.IsMatch(context));
            var typeMap = mapper.ConfigurationProvider.CreateTypeMap(context.SourceType, context.DestinationType, _profileName);

            context = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType, context.DestinationType);

            var map = context.Engine.GetOrAddMapper(contextTypePair, missFunc);

            return(map.Map(context, mapper));
        }
Exemplo n.º 46
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (mapper == null) throw new ArgumentNullException("mapper");

            if (context.SourceValue == null && !mapper.ShouldMapSourceValueAsNull(context))
            {
                return mapper.CreateObject(context);
            }

            return context.SourceValue;
        }
Exemplo n.º 47
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (!IsMatch(context) || context.SourceValue == null)
                return null;
            
            var nvc = new NameValueCollection();
            var source = context.SourceValue as NameValueCollection;
            foreach (var s in source.AllKeys)
                nvc.Add(s, source[s]);

            return nvc;
        }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            Type genericType = typeof(EnumerableMapper <>);

            var elementType = TypeHelper.GetElementType(context.DestinationType);

            var enumerableMapper = genericType.MakeGenericType(elementType);

            var objectMapper = (IObjectMapper)Activator.CreateInstance(enumerableMapper);

            return(objectMapper.Map(context, mapper));
        }
Exemplo n.º 49
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context))
            {
                return(null);
            }

            var sourceEnumerableValue          = (IEnumerable)context.SourceValue ?? new object[0];
            IEnumerable <object> keyValuePairs = sourceEnumerableValue.Cast <object>();

            Type genericSourceDictType = context.SourceType.GetDictionaryType();
            Type sourceKeyType         = genericSourceDictType.GetGenericArguments()[0];
            Type sourceValueType       = genericSourceDictType.GetGenericArguments()[1];
            Type sourceKvpType         = KvpType.MakeGenericType(sourceKeyType, sourceValueType);
            Type genericDestDictType   = context.DestinationType.GetDictionaryType();
            Type destKeyType           = genericDestDictType.GetGenericArguments()[0];
            Type destValueType         = genericDestDictType.GetGenericArguments()[1];

            var dictionaryEntries = keyValuePairs.OfType <DictionaryEntry>();

            if (dictionaryEntries.Any())
            {
                keyValuePairs = dictionaryEntries.Select(e => Activator.CreateInstance(sourceKvpType, e.Key, e.Value));
            }

            object destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType);
            int    count          = 0;

            foreach (object keyValuePair in keyValuePairs)
            {
                object sourceKey   = sourceKvpType.GetProperty("Key").GetValue(keyValuePair, new object[0]);
                object sourceValue = sourceKvpType.GetProperty("Value").GetValue(keyValuePair, new object[0]);

                TypeMap keyTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceKey, null, sourceKeyType,
                                                                                 destKeyType);
                TypeMap valueTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceValue, null, sourceValueType,
                                                                                   destValueType);

                ResolutionContext keyContext = context.CreateElementContext(keyTypeMap, sourceKey, sourceKeyType,
                                                                            destKeyType, count);
                ResolutionContext valueContext = context.CreateElementContext(valueTypeMap, sourceValue, sourceValueType,
                                                                              destValueType, count);

                object destKey   = mapper.Map(keyContext);
                object destValue = mapper.Map(valueContext);

                genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] { destKey, destValue });

                count++;
            }

            return(destDictionary);
        }
Exemplo n.º 50
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if(context == null) throw new ArgumentNullException("context");

            context.TypeMap.Seal();

            var mapperToUse = _mappers.First(objectMapper => objectMapper.IsMatch(context, mapper));
            object mappedObject = mapperToUse.Map(context, mapper);

            context.TypeMap.AfterMap(context.SourceValue, mappedObject);
            return mappedObject;
        }
Exemplo n.º 51
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            Type enumDestType = TypeHelper.GetEnumerationType(context.DestinationType);

            if (context.SourceValue == null)
            {
                return mapper.CreateObject(context);
            }

            Type enumSourceType = TypeHelper.GetEnumerationType(context.SourceType);

            return Enum.Parse(enumDestType, Enum.GetName(enumSourceType, context.SourceValue));
        }
Exemplo n.º 52
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (mapper == null) throw new ArgumentNullException("mapper");

            if (context.SourceValue == null)
            {
                return mapper.CreateObject(context);
            }

            TypeConverter typeConverter = GetTypeConverter(context);
            return typeConverter.ConvertTo(context.SourceValue, context.DestinationType);
        }
Exemplo n.º 53
0
        static IEnumerable LoadDataReaderViaList(IDataReader dataReader, IMappingEngineRunner mapper, Build buildFrom, ResolutionContext resolveUsingContext, Type elementType)
        {
            var list = ObjectCreator.CreateList(elementType);

            while (dataReader.Read())
            {
                var result = buildFrom(dataReader);
                MapPropertyValues(resolveUsingContext, mapper, result);
                list.Add(result);
            }

            return(list);
        }
 public object Map(ResolutionContext context, IMappingEngineRunner mapper)
 {
     var dictionary = (StringDictionary)context.SourceValue;
     object destination = mapper.CreateObject(context);
     var destTypeDetails = new TypeDetails(context.DestinationType, _ => true, _ => true);
     var members = from name in dictionary.Keys join member in destTypeDetails.PublicWriteAccessors on name equals member.Name select member;
     foreach(var member in members)
     {
         object value = ReflectionHelper.Map(member, dictionary[member.Name]);
         member.SetMemberValue(destination, value);
     }
     return destination;
 }
Exemplo n.º 55
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (mapper == null) throw new ArgumentNullException("mapper");

            bool toEnum = false;
            Type enumSourceType = TypeHelper.GetEnumerationType(context.SourceType);
            Type enumDestinationType = TypeHelper.GetEnumerationType(context.DestinationType);

            if (EnumToStringMapping(context, ref toEnum))
            {
                if (toEnum)
                {
                    return Enum.Parse(enumDestinationType, context.SourceValue.ToString(), true);
                }
                return Enum.GetName(enumSourceType, context.SourceValue);
            }
            if (EnumToEnumMapping(context))
            {
                if (context.SourceValue == null)
                {
                    return mapper.CreateObject(context);
                }

                if (!Enum.IsDefined(enumSourceType, context.SourceValue))
                {
                    return Enum.ToObject(enumDestinationType, context.SourceValue);
                }

            #if !SILVERLIGHT
                if (!Enum.GetNames(enumDestinationType).Contains(context.SourceValue.ToString()))
                {
                    Type underlyingSourceType = Enum.GetUnderlyingType(enumSourceType);
                    var underlyingSourceValue = Convert.ChangeType(context.SourceValue, underlyingSourceType);

                    return Enum.ToObject(context.DestinationType, underlyingSourceValue);
                }
            #endif

                return Enum.Parse(enumDestinationType, Enum.GetName(enumSourceType, context.SourceValue), true);
            }
            if (EnumToUnderlyingTypeMapping(context, ref toEnum))
            {
                if (toEnum)
                {
                    return Enum.Parse(enumDestinationType, context.SourceValue.ToString(), true);
                }
                return Convert.ChangeType(context.SourceValue, context.DestinationType, null);
            }
            return null;
        }
Exemplo n.º 56
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            Hashtable sourceHashtable = (Hashtable)context.SourceValue ?? new Hashtable();
            object destinationObject = ObjectCreator.CreateObject(context.DestinationType);

            foreach (DictionaryEntry entry in sourceHashtable)
            {
                if (entry.Value == null)
                    continue;

                object sourceValue = entry.Value;
                Type sourceValueType = sourceValue.GetType();

                var propertyInfo = context.DestinationType.GetProperty(entry.Key.ToString());
                if (propertyInfo == null)
                    continue;

                var setter = propertyInfo.GetSetMethod(true);
                if (setter == null)
                    continue;

                Type destinationMemberType = propertyInfo.PropertyType;

                // try to set object as is
                try
                {
                    // if needs to be casted:
                    if (destinationMemberType != sourceValueType)
                        sourceValue = Convert.ChangeType(entry.Value, destinationMemberType);

                    setter.Invoke(destinationObject, new[] { sourceValue });
                    continue;
                }
                catch (Exception e)
                {
                }

                // swallowed, try to leverage type converters.
                var converter = TypeDescriptor.GetConverter(destinationMemberType);
                if (converter == null || !converter.CanConvertFrom(sourceValueType))
                    continue;

                var destValue = converter.ConvertFrom(entry.Value);
                setter.Invoke(destinationObject, new[] { destValue });
            }

            MapPropertyValues(context, mapper, destinationObject);

            return destinationObject;
        }
Exemplo n.º 57
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (mapper == null) throw new ArgumentNullException("mapper");

            Type enumDestType = TypeHelper.GetEnumerationType(context.DestinationType);

            if (context.SourceValue == null)
            {
                return mapper.CreateObject(context);
            }

            return Enum.Parse(enumDestType, context.SourceValue.ToString(), true);
        }
Exemplo n.º 58
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context))
                return null;

            var sourceEnumerableValue = (IEnumerable) context.SourceValue ?? new object[0];
            IEnumerable<object> keyValuePairs = sourceEnumerableValue.Cast<object>();

            Type genericSourceDictType = context.SourceType.GetDictionaryType();
            Type sourceKeyType = genericSourceDictType.GetGenericArguments()[0];
            Type sourceValueType = genericSourceDictType.GetGenericArguments()[1];
            Type sourceKvpType = KvpType.MakeGenericType(sourceKeyType, sourceValueType);
            Type genericDestDictType = context.DestinationType.GetDictionaryType();
            Type destKeyType = genericDestDictType.GetGenericArguments()[0];
            Type destValueType = genericDestDictType.GetGenericArguments()[1];

            var dictionaryEntries = keyValuePairs.OfType<DictionaryEntry>();
            if (dictionaryEntries.Any())
                keyValuePairs = dictionaryEntries.Select(e => Activator.CreateInstance(sourceKvpType, e.Key, e.Value));

            object destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType);
            int count = 0;

            foreach (object keyValuePair in keyValuePairs)
            {
                object sourceKey = sourceKvpType.GetProperty("Key").GetValue(keyValuePair, new object[0]);
                object sourceValue = sourceKvpType.GetProperty("Value").GetValue(keyValuePair, new object[0]);

                TypeMap keyTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceKey, null, sourceKeyType,
                    destKeyType);
                TypeMap valueTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceValue, null, sourceValueType,
                    destValueType);

                ResolutionContext keyContext = context.CreateElementContext(keyTypeMap, sourceKey, sourceKeyType,
                    destKeyType, count);
                ResolutionContext valueContext = context.CreateElementContext(valueTypeMap, sourceValue, sourceValueType,
                    destValueType, count);

                object destKey = mapper.Map(keyContext);
                object destValue = mapper.Map(valueContext);

                genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] {destKey, destValue});

                count++;
            }

            return destDictionary;
        }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var collection = context.SourceValue as SPListItemCollection;

            //1. Need to create single item type
            var elementType = context.DestinationType.GetGenericArguments()[0];

            //2. Need to create result with generic type
            var resultCollection = Activator.CreateInstance(context.DestinationType) as IList;

            foreach (var item in collection)
            {
                var mappedItem = Mapper.Map(item, typeof(SPListItem), elementType);
                resultCollection.Add(mappedItem);
            }
            return resultCollection;
        }
			public object Map(ResolutionContext context, IMappingEngineRunner mapper)
			{
				var mappedObject = GetMappedObject(context, mapper);
				if (context.SourceValue != null && !context.Options.DisableCache)
                    context.InstanceCache[context] = mappedObject;

				context.TypeMap.BeforeMap(context.SourceValue, mappedObject);

                foreach (PropertyMap propertyMap in context.TypeMap.GetPropertyMaps())
                {
                    MapPropertyValue(context.CreatePropertyMapContext(propertyMap), mapper, mappedObject, propertyMap);
                }
                mappedObject = ReassignValue(context, mappedObject);

                context.TypeMap.AfterMap(context.SourceValue, mappedObject);

                return mappedObject;
			}