private static Array Map<TDestination, TSource, TSourceElement>(TSource source, ResolutionContext context)
            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, null));
            }
            return destinationArray;
        }
 protected override void Because_of()
 {
     for (var index = 0; index < _source.Length; index++)
     {
         _filler.NewValue(index);
     }
 }
Пример #3
0
            private static Array MapMultidimensional(Array source, Type destinationElementType, ResolutionContext context)
            {
                var sourceElementType = source.GetType().GetElementType();
                var destinationArray  = Array.CreateInstance(destinationElementType, Enumerable.Range(0, source.Rank).Select(source.GetLength).ToArray());
                var filler            = new MultidimensionalArrayFiller(destinationArray);

                foreach (var item in source)
                {
                    filler.NewValue(context.Map(item, null, sourceElementType, destinationElementType, null));
                }
                return(destinationArray);
            }