private static Array Map <TDestination, TSource, TSourceElement>(TSource source, ResolutionContext context, ProfileMap profileMap) where TSource : IEnumerable { var destElementType = ElementTypeHelper.GetElementType(typeof(TDestination)); if (typeof(TDestination).IsAssignableFrom(typeof(TSource))) { var elementTypeMap = context.ConfigurationProvider.ResolveTypeMap(typeof(TSourceElement), destElementType); if (elementTypeMap == null) { return(source as Array); } } var sourceList = (IEnumerable)source; var sourceArray = source as Array; var destinationArray = sourceArray == null ? Array.CreateInstance(destElementType, sourceList.Cast <object>().Count()) : Array.CreateInstance(destElementType, Enumerable.Range(0, sourceArray.Rank).Select(sourceArray.GetLength).ToArray()); var filler = new MultidimensionalArrayFiller(destinationArray); foreach (var item in sourceList) { filler.NewValue(context.Map(item, null, typeof(TSourceElement), destElementType)); } return(destinationArray); }
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); }
protected override void SetElementValue(Array destination, object mappedValue, int index) { filler.NewValue(mappedValue); }