protected void DefaultFullMapping( ref object destination, Type destType, TypeInfo destTypeInfo, object source, Type srcType, TypeInfo srcTypeInfo, Configuration config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } var actAsCollection = false; var isDestinationFilled = destination == null; Type destGenericParameter = null; if (destination == null) { actAsCollection = typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(destTypeInfo) && source is IEnumerable; if (actAsCollection) { var resolvedType = TypeResolver.TryCreateInstance(destType); if (resolvedType == null) { if (destTypeInfo.IsSubclassOf(typeof(IDictionary))) { var enumerableTypes = destTypeInfo.ImplementedInterfaces .Where(t => t.GetGenericTypeDefinition() == typeof(IDictionary <,>)) .Select(t => t.GenericTypeArguments[0]) .ToList(); var enumerableTypesCount = enumerableTypes.Count; if (enumerableTypesCount > 2) { throw new InvalidOperationException( Localization.Localization .Format <IAutomationLocalization, DefaultAutomationLocalization>( x => x.ThereIsMoreThanOneEnumerableImplementationInDestinationType)); } if (enumerableTypesCount == 2) { //destGenericParameter = enumerableTypes[0]; var dictType = typeof(Dictionary <,>).MakeGenericType(enumerableTypes.ToArray()); resolvedType = TypeResolver.TryCreateInstance(dictType); if (resolvedType == null) { throw new InvalidOperationException( Localization.Localization .Format <IAutomationLocalization, DefaultAutomationLocalization>( x => x.UnableToCreateAnInstanceOfDestination)); } } else { resolvedType = new WeakTypeDictionary(); } } else { var enumerableType = destTypeInfo.IsGenericType && destType.GetGenericTypeDefinition() == typeof(IEnumerable <>) ? destTypeInfo.GenericTypeArguments[0] : destTypeInfo.ImplementedInterfaces .Where(t => { var typeInfo = t.GetTypeInfo(); return(typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable <>)); }) .Select(t => t.GenericTypeArguments[0]) .FirstOrDefault(); if (enumerableType != null) { destGenericParameter = enumerableType; var listType = typeof(List <>).MakeGenericType(enumerableType); resolvedType = TypeResolver.TryCreateInstance(listType); if (resolvedType == null) { throw new InvalidOperationException( Localization.Localization .Format <IAutomationLocalization, DefaultAutomationLocalization>( x => x.UnableToCreateAnInstanceOfDestination)); } } else { resolvedType = new WeakTypeCollection(); } } } destination = resolvedType; } else { destination = TypeResolver.TryCreateInstance(destType); } } DefaultPropertyMapping(destination, destType, destTypeInfo, source, srcType, srcTypeInfo, config); if (actAsCollection) { DefaultCollectionMapping(destination, destType, destTypeInfo, destGenericParameter, source, srcType, srcTypeInfo, config, isDestinationFilled); } }
private void _map(MapperCacheContext cacheContext, ref object destination, Type destType, TypeInfo destTypeInfo, object source, Type srcType, TypeInfo srcTypeInfo, MapperContext context, int cLevel, int topLevel) { //if (dest == null) throw new ArgumentNullException(nameof(dest)); if (context == null) { throw new ArgumentNullException(nameof(context)); } var actAsCollection = false; var isDestinationFilled = destination == null; Type destGenericParameter = null; var resolver = context.TypeResolver; if (destination == null) { actAsCollection = typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(destTypeInfo) && source is IEnumerable; if (actAsCollection) { object resolvedType = null; if (destTypeInfo.IsClass) { resolvedType = resolver.Resolve(destType); } else { bool useAsGenericCollection = false; Type[] genericCollectionParameterTypes = null; bool useAsGenericDictionary = false; Type[] genericDictionaryParameterTypes = null; foreach (var gDestTypeInterface in destTypeInfo.ImplementedInterfaces) { var tpInfo = gDestTypeInterface.GetGenericTypeDefinition().GetTypeInfo(); if (tpInfo.IsSubclassOf(typeof(IDictionary <,>))) { useAsGenericDictionary = true; genericDictionaryParameterTypes = tpInfo.GenericTypeArguments; break; } else if (!useAsGenericCollection && tpInfo.IsSubclassOf(typeof(IEnumerable <>))) { useAsGenericCollection = true; genericCollectionParameterTypes = tpInfo.GenericTypeArguments; } } if (useAsGenericDictionary) { var dictType = typeof(Dictionary <,>).MakeGenericType(genericDictionaryParameterTypes); resolvedType = resolver.Resolve(dictType); if (resolvedType == null) { throw new InvalidOperationException( Localization.Localization .Format <IAutomationLocalization, DefaultAutomationLocalization>( x => x.UnableToCreateAnInstanceOfDestination)); } } else if (destTypeInfo.IsSubclassOf(typeof(IDictionary))) { resolvedType = new WeakTypeDictionary(); } if (useAsGenericCollection) { var listType = typeof(List <>).MakeGenericType(genericCollectionParameterTypes); resolvedType = resolver.Resolve(listType); if (resolvedType == null) { throw new InvalidOperationException( Localization.Localization .Format <IAutomationLocalization, DefaultAutomationLocalization>( x => x.UnableToCreateAnInstanceOfDestination)); } } else { resolvedType = new WeakTypeCollection(); } } destination = resolvedType; } else { destination = resolver.Resolve(destType); } } DefaultPropertyMapping(cacheContext, destination, destType, destTypeInfo, source, srcType, srcTypeInfo, context, cLevel, topLevel); if (actAsCollection) { DefaultCollectionMapping(cacheContext, destination, destType, destTypeInfo, destGenericParameter, source, srcType, srcTypeInfo, context, cLevel, topLevel, isDestinationFilled); } }