public object Map(ResolutionContext context)
        {
            if (context.IsSourceValueNull && context.Engine.ShouldMapSourceCollectionAsNull(context))
            {
                return(null);
            }

            ICollection <object> enumerableValue = ((IEnumerable)context.SourceValue ?? new object[0])
                                                   .Cast <object>()
                                                   .ToList();

            Type sourceElementType = TypeHelper.GetElementType(context.SourceType, enumerableValue);
            Type destElementType   = TypeHelper.GetElementType(context.DestinationType);

            // If you can just assign the collection from one side to the other and the element types don't need to be mapped
            if (ShouldAssignEnumerable(context))
            {
                var elementTypeMap = context.ConfigurationProvider.ResolveTypeMap(sourceElementType, destElementType);
                if (elementTypeMap == null)
                {
                    return(context.SourceValue);
                }
            }

            var sourceLength = enumerableValue.Count;
            var destination  = GetOrCreateDestinationObject(context, destElementType, sourceLength);
            var enumerable   = GetEnumerableFor(destination);

            ClearEnumerable(enumerable);

            int i = 0;

            foreach (object item in enumerableValue)
            {
                var newContext = context.CreateElementContext(null, item, sourceElementType, destElementType, i);
                var elementResolutionResult = new ResolutionResult(newContext);

                var typeMap = context.ConfigurationProvider.ResolveTypeMap(elementResolutionResult, destElementType);

                Type targetSourceType      = typeMap != null ? typeMap.SourceType : sourceElementType;
                Type targetDestinationType = typeMap != null ? typeMap.DestinationType : destElementType;

                newContext = context.CreateElementContext(typeMap, item, targetSourceType, targetDestinationType, i);

                object mappedValue = context.Engine.Map(newContext);

                SetElementValue(enumerable, mappedValue, i);

                i++;
            }

            object valueToAssign = destination;

            return(valueToAssign);
        }
Example #2
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)
        {
            var runner = context.MapperContext.Runner;

            if (context.IsSourceValueNull && runner.ShouldMapSourceCollectionAsNull(context))
            {
                return null;
            }

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

            var kvpEnumerator = GetKeyValuePairEnumerator(context, sourceKvpType);
            var destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType);

            var count = 0;

            while (kvpEnumerator.MoveNext())
            {
                var keyValuePair = kvpEnumerator.Current;

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

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

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

                var destKey = runner.Map(keyContext);
                var destValue = runner.Map(valueContext);

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

                count++;
            }

            return destDictionary;
        }
        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;
        }
Example #5
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context))
            {
                return(null);
            }

            ICollection <object> enumerableValue = ((IEnumerable)context.SourceValue ?? new object[0])
                                                   .Cast <object>()
                                                   .ToList();

            Type sourceElementType = TypeHelper.GetElementType(context.SourceType, enumerableValue);
            Type destElementType   = TypeHelper.GetElementType(context.DestinationType);

            var sourceLength = enumerableValue.Count;
            var destination  = GetOrCreateDestinationObject(context, mapper, destElementType, sourceLength);
            var enumerable   = GetEnumerableFor(destination);

            ClearEnumerable(enumerable);

            int i = 0;

            foreach (object item in enumerableValue)
            {
                var newContext = context.CreateElementContext(null, item, sourceElementType, destElementType, i);
                var elementResolutionResult = new ResolutionResult(newContext);

                var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(elementResolutionResult, destElementType);

                Type targetSourceType      = typeMap != null ? typeMap.SourceType : sourceElementType;
                Type targetDestinationType = typeMap != null ? typeMap.DestinationType : destElementType;

                newContext = context.CreateElementContext(typeMap, item, targetSourceType, targetDestinationType, i);

                object mappedValue = mapper.Map(newContext);

                SetElementValue(enumerable, mappedValue, i);

                i++;
            }

            object valueToAssign = destination;

            return(valueToAssign);
        }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context))
            {
                return(null);
            }
            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 kvpEnumerator  = GetKeyValuePairEnumerator(context, sourceKvpType);
            var destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType);
            int count          = 0;

            while (kvpEnumerator.MoveNext())
            {
                var    keyValuePair = kvpEnumerator.Current;
                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);
        }
Example #7
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var sourceValue = (IEnumerable)context.SourceValue ?? new object[0];
            IEnumerable <object> enumerableValue = sourceValue.Cast <object>();

            Type sourceElementType = TypeHelper.GetElementType(context.SourceType, sourceValue);
            Type destElementType   = TypeHelper.GetElementType(context.DestinationType);

            var sourceLength = enumerableValue.Count();
            var destination  = (context.DestinationValue ?? CreateDestinationObject(context, destElementType, sourceLength, mapper));
            var enumerable   = GetEnumerableFor(destination);

            ClearEnumerable(enumerable);

            int i = 0;

            foreach (object item in enumerableValue)
            {
                var newContext = context.CreateElementContext(null, item, sourceElementType, destElementType, i);
                var elementResolutionResult = new ResolutionResult(newContext);

                var typeMap = mapper.ConfigurationProvider.FindTypeMapFor(elementResolutionResult, destElementType);

                Type targetSourceType      = typeMap != null ? typeMap.SourceType : sourceElementType;
                Type targetDestinationType = typeMap != null ? typeMap.DestinationType : destElementType;

                newContext = context.CreateElementContext(typeMap, item, targetSourceType, targetDestinationType, i);

                object mappedValue = mapper.Map(newContext);

                SetElementValue(enumerable, mappedValue, i);

                i++;
            }

            object valueToAssign = destination;

            return(valueToAssign);
        }
        public object Map(ResolutionContext context)
        {
            var runner = context.MapperContext.Runner;
            var sourceEnumerableValue = (IEnumerable)context.SourceValue ?? new object[0];
            var enumerableValue = sourceEnumerableValue.Cast<object>();

            var sourceElementType = TypeHelper.GetElementType(context.SourceType, sourceEnumerableValue);
            var genericDestDictType = context.DestinationType.GetDictionaryType();
            var  destKeyType = genericDestDictType.GetGenericArguments()[0];
            var destValueType = genericDestDictType.GetGenericArguments()[1];
            var destKvpType = KvpType.MakeGenericType(destKeyType, destValueType);

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

            foreach (var item in enumerableValue)
            {
                var typeMap = runner.ConfigurationProvider.ResolveTypeMap(item, null, sourceElementType, destKvpType);

                var targetSourceType = typeMap != null ? typeMap.SourceType : sourceElementType;
                var targetDestinationType = typeMap != null ? typeMap.DestinationType : destKvpType;

                var newContext = context.CreateElementContext(typeMap, item, targetSourceType, targetDestinationType,
                    count);

                var mappedValue = runner.Map(newContext);
                var keyProperty = mappedValue.GetType().GetProperty("Key");
                var destKey = keyProperty.GetValue(mappedValue, null);

                var valueProperty = mappedValue.GetType().GetProperty("Value");
                var destValue = valueProperty.GetValue(mappedValue, null);

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

                count++;
            }

            return destDictionary;
        }
Example #9
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var sourceEnumerableValue = (IEnumerable)context.SourceValue ?? new object[0];
            var enumerableValue       = sourceEnumerableValue.Cast <object>();

            var sourceElementType   = TypeHelper.GetElementType(context.SourceType, sourceEnumerableValue);
            var genericDestDictType = PrimitiveExtensions.GetDictionaryType(context.DestinationType);
            var destKeyType         = genericDestDictType.GetGenericArguments()[0];
            var destValueType       = genericDestDictType.GetGenericArguments()[1];
            var destKvpType         = KvpType.MakeGenericType(destKeyType, destValueType);

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

            foreach (var item in enumerableValue)
            {
                var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(item, null, sourceElementType, destKvpType);

                var targetSourceType      = typeMap != null ? typeMap.SourceType : sourceElementType;
                var targetDestinationType = typeMap != null ? typeMap.DestinationType : destKvpType;

                var newContext = context.CreateElementContext(typeMap, item, targetSourceType, targetDestinationType,
                                                              count);

                var mappedValue = mapper.Map(newContext);
                var keyProperty = mappedValue.GetType().GetProperty("Key");
                var destKey     = keyProperty.GetValue(mappedValue, null);

                var valueProperty = mappedValue.GetType().GetProperty("Value");
                var destValue     = valueProperty.GetValue(mappedValue, null);

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

                count++;
            }

            return(destDictionary);
        }