예제 #1
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context))
            {
                return(null);
            }
            var genericSourceDictType = PrimitiveExtensions.GetDictionaryType(context.SourceType);
            var sourceKeyType         = genericSourceDictType.GetGenericArguments()[0];
            var sourceValueType       = genericSourceDictType.GetGenericArguments()[1];
            var sourceKvpType         = KvpType.MakeGenericType(sourceKeyType, sourceValueType);
            var genericDestDictType   = PrimitiveExtensions.GetDictionaryType(context.DestinationType);
            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 = mapper.ConfigurationProvider.ResolveTypeMap(sourceKey, null, sourceKeyType,
                                                                             destKeyType);
                var valueTypeMap = mapper.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   = mapper.Map(keyContext);
                var destValue = mapper.Map(valueContext);

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

                count++;
            }

            return(destDictionary);
        }
예제 #2
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);
        }