Esempio n. 1
0
        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 object GetOrCreateDestinationObject(ResolutionContext context, Type destElementType, int sourceLength)
        {
            var sourceArray = context.SourceValue as Array;

            if (sourceArray == null)
            {
                return(ObjectCreator.CreateArray(destElementType, sourceLength));
            }
            var destinationArray = ObjectCreator.CreateArray(destElementType, sourceArray);

            filler = new MultidimensionalArrayFiller(destinationArray);
            return(destinationArray);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
 protected override object GetOrCreateDestinationObject(ResolutionContext context, IMappingEngineRunner mapper, Type destElementType, int sourceLength)
 {
     return(ObjectCreator.CreateArray(destElementType, sourceLength));
 }
Esempio n. 6
0
 protected override Array CreateDestinationObjectBase(Type destElementType, int sourceLength)
 {
     return(ObjectCreator.CreateArray(destElementType, sourceLength));
 }