Exemplo n.º 1
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.º 2
0
        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);
        }
Exemplo n.º 3
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var sourceEnumerableValue            = (IEnumerable)context.SourceValue ?? new object[0];
            IEnumerable <object> enumerableValue = sourceEnumerableValue.Cast <object>();

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

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

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

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

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

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

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

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

                count++;
            }

            return(destDictionary);
        }