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

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

            Array sourceArray = (Array) context.SourceValue ?? ObjectCreator.CreateArray(sourceElementType, 0);

            int sourceLength = sourceArray.Length;
            Array destArray = ObjectCreator.CreateArray(destElementType, sourceLength);

            Array.Copy(sourceArray, destArray, sourceLength);

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

            var sourceElementType = TypeHelper.GetElementType(context.SourceType);
            var destElementType   = TypeHelper.GetElementType(context.DestinationType);

            var sourceArray = (Array)context.SourceValue ?? ObjectCreator.CreateArray(sourceElementType, 0);

            var sourceLength = sourceArray.Length;
            var destArray    = ObjectCreator.CreateArray(destElementType, sourceLength);

            Array.Copy(sourceArray, destArray, sourceLength);

            return(destArray);
        }