public static TDestination Map <TSource, TDestination, TDestinationElement>(TSource source, TDestination destination, ResolutionContext context) where TSource : class, IEnumerable where TDestination : class, IEnumerable { if (source == null && context.Mapper.ShouldMapSourceCollectionAsNull(context)) { return(null); } var sourceElementType = TypeHelper.GetElementType(typeof(TSource), source); var destElementType = typeof(TDestinationElement); source = source ?? (context.ConfigurationProvider.AllowNullDestinationValues ? ObjectCreator.CreateNonNullValue(typeof(TSource)) : ObjectCreator.CreateObject(typeof(TSource))) as TSource; TDestination destEnumeration = (destination is IList && !(destination is Array)) ? destination : (TDestination)ObjectCreator.CreateList(destElementType); var list = destEnumeration as IList <TDestinationElement>; list.Clear(); foreach (var item in source) { list.Add((TDestinationElement)context.Mapper.Map(item, null, sourceElementType, destElementType, context)); } return(destEnumeration); }
public static TDestination Map <TSource, TSourceItem, TDestination, TDestinationItem>(TSource source, TDestination destination, ResolutionContext context) where TSource : IEnumerable where TDestination : class, ICollection <TDestinationItem> { if (source == null && context.Mapper.ShouldMapSourceCollectionAsNull(context)) { return(null); } TDestination list = destination ?? ( typeof(TDestination).IsInterface() ? new List <TDestinationItem>() as TDestination : (TDestination)(context.ConfigurationProvider.AllowNullDestinationValues ? ObjectCreator.CreateNonNullValue(typeof(TDestination)) : ObjectCreator.CreateObject(typeof(TDestination)))); list.Clear(); var itemContext = new ResolutionContext(context); foreach (var item in (IEnumerable)source ?? Enumerable.Empty <object>()) { list.Add((TDestinationItem)itemContext.Map(item, default(TDestinationItem), typeof(TSourceItem), typeof(TDestinationItem))); } return(list); }
public static TDestination Map <TSource, TSourceKey, TSourceValue, TDestination, TDestinationKey, TDestinationValue>(TSource source, TDestination destination, ResolutionContext context) where TSource : IDictionary <TSourceKey, TSourceValue> where TDestination : class, IDictionary <TDestinationKey, TDestinationValue> { if (source == null && context.Mapper.ShouldMapSourceCollectionAsNull(context)) { return(null); } TDestination list = destination ?? ( typeof(TDestination).IsInterface() ? new Dictionary <TDestinationKey, TDestinationValue>() as TDestination : (TDestination)(context.ConfigurationProvider.AllowNullDestinationValues ? ObjectCreator.CreateNonNullValue(typeof(TDestination)) : ObjectCreator.CreateObject(typeof(TDestination)))); list.Clear(); var itemContext = new ResolutionContext(context); foreach (var keyPair in (IEnumerable <KeyValuePair <TSourceKey, TSourceValue> >)source ?? Enumerable.Empty <KeyValuePair <TSourceKey, TSourceValue> >()) { list.Add((TDestinationKey)itemContext.Map(keyPair.Key, default(TDestinationKey), typeof(TSourceKey), typeof(TDestinationKey)), (TDestinationValue)itemContext.Map(keyPair.Value, default(TDestinationValue), typeof(TSourceValue), typeof(TDestinationValue))); } return(list); }
public static TDestination Map <TSource, TDestination>(TSource source, TDestination destination, ResolutionContext context) where TDestination : DynamicObject { if (destination == null) { destination = (TDestination)(!context.ConfigurationProvider.AllowNullDestinationValues ? ObjectCreator.CreateNonNullValue(typeof(TDestination)) : ObjectCreator.CreateObject(typeof(TDestination))); } var memberContext = new ResolutionContext(context); foreach (var member in new TypeDetails(typeof(TSource)).PublicWriteAccessors) { object sourceMemberValue; try { sourceMemberValue = member.GetMemberValue(source); } catch (RuntimeBinderException) { continue; } var destinationMemberValue = memberContext.Map(member, sourceMemberValue); SetDynamically(member, destination, destinationMemberValue); } return(destination); }
public object Map(ResolutionContext context, IMappingEngineRunner mapper) { if (context.SourceValue == null && !mapper.ShouldMapSourceCollectionAsNull(context)) { return(ObjectCreator.CreateObject(context.DestinationType)); } return(context.SourceValue); }
private static TDestination Map <TSource, TDestination>(TSource source, ResolutionContext context) { if (source == null) { return((TDestination)(context.ConfigurationProvider.AllowNullDestinationValues ? ObjectCreator.CreateNonNullValue(typeof(TDestination)) : ObjectCreator.CreateObject(typeof(TDestination)))); } return(GetConverter <TSource, TDestination>(source)); }
public static Array Map <TDestination, TSource, TSourceElement>(TSource source, ResolutionContext context) where TSource : IEnumerable { if (source == null && context.Mapper.ShouldMapSourceCollectionAsNull(context)) { return(null); } var destElementType = TypeHelper.GetElementType(typeof(TDestination)); if (!context.IsSourceValueNull && context.DestinationType.IsAssignableFrom(context.SourceType)) { var elementTypeMap = context.ConfigurationProvider.ResolveTypeMap(typeof(TSourceElement), destElementType); if (elementTypeMap == null) { return(source as Array); } } IEnumerable sourceList = source; if (sourceList == null) { sourceList = typeof(TSource).GetTypeInfo().IsInterface ? new List <TSourceElement>() : (IEnumerable <TSourceElement>)(context.ConfigurationProvider.AllowNullDestinationValues ? ObjectCreator.CreateNonNullValue(typeof(TSource)) : ObjectCreator.CreateObject(typeof(TSource))); } var sourceLength = sourceList.OfType <object>().Count(); var sourceArray = context.SourceValue as Array; Array destinationArray; if (sourceArray == null) { destinationArray = ObjectCreator.CreateArray(destElementType, sourceLength); } else { destinationArray = ObjectCreator.CreateArray(destElementType, sourceArray); filler = new MultidimensionalArrayFiller(destinationArray); } int count = 0; foreach (var item in sourceList) { filler.NewValue(context.Mapper.Map(item, null, typeof(TSourceElement), destElementType, context)); } return(destinationArray); }
public static TDestination Map <TSource, TDestination>(TSource source, ResolutionContext context) where TDestination : struct { Type enumDestType = TypeHelper.GetEnumerationType(typeof(TDestination)); if (source == null) { return((TDestination)(context.ConfigurationProvider.AllowNullDestinationValues ? ObjectCreator.CreateNonNullValue(typeof(TDestination)) : ObjectCreator.CreateObject(typeof(TDestination)))); } return((TDestination)Enum.Parse(enumDestType, source.ToString(), true)); }
public static TDestination Map <TDestination, TSource, TSourceElement>(TSource source, ResolutionContext context) where TSource : IEnumerable where TDestination : class { if (source == null && context.Mapper.ShouldMapSourceCollectionAsNull(context)) { return(null); } var destElementType = TypeHelper.GetElementType(typeof(TDestination)); if (!context.IsSourceValueNull && context.DestinationType.IsAssignableFrom(context.SourceType)) { var elementTypeMap = context.ConfigurationProvider.ResolveTypeMap(typeof(TSourceElement), destElementType); if (elementTypeMap == null) { return(source as TDestination); } } IEnumerable sourceList = source; if (sourceList == null) { sourceList = typeof(TSource).GetTypeInfo().IsInterface ? new List <TSourceElement>() : (IEnumerable <TSourceElement>)(context.ConfigurationProvider.AllowNullDestinationValues ? ObjectCreator.CreateNonNullValue(typeof(TSource)) : ObjectCreator.CreateObject(typeof(TSource))); } var sourceLength = sourceList.OfType <object>().Count(); Array array = ObjectCreator.CreateArray(destElementType, sourceLength); int count = 0; var itemContext = new ResolutionContext(context); foreach (var item in sourceList) { array.SetValue(itemContext.Map(item, null, typeof(TSourceElement), destElementType), count++); } return(array as TDestination); }