Пример #1
0
        private static CollectionAdapterModel CreateCollectionAdapterModel()
        {
            var cam = new CollectionAdapterModel();

            var config    = TypeAdapterConfig <TSource, TDestination> .Configuration;
            var hasConfig = config != null;

            var sourceElementType      = ReflectionUtils.ExtractElementType(typeof(TSource));
            var destinationElementType = typeof(TDestinationElementType);

            if (ReflectionUtils.IsPrimitive(destinationElementType) || destinationElementType == typeof(object))
            {
                cam.IsPrimitive = true;

                var converter = ReflectionUtils.CreatePrimitiveConverter(sourceElementType, destinationElementType);
                if (converter != null)
                {
                    cam.AdaptInvoker = converter;
                }
            }
            else if (ReflectionUtils.IsCollection(destinationElementType))
            {
                var methodInfo = typeof(CollectionAdapter <, ,>).MakeGenericType(sourceElementType, ReflectionUtils.ExtractElementType(destinationElementType), destinationElementType).GetMethod("Adapt", new Type[] { sourceElementType, typeof(Dictionary <,>).MakeGenericType(typeof(int), typeof(int)) });
                cam.AdaptInvoker = FastInvoker.GetMethodInvoker(methodInfo);
            }
            else
            {
                var methodInfo = typeof(ClassAdapter <,>).MakeGenericType(sourceElementType, destinationElementType).GetMethod("Adapt", new Type[] { sourceElementType, typeof(Dictionary <,>).MakeGenericType(typeof(int), typeof(int)) });
                cam.AdaptInvoker = FastInvoker.GetMethodInvoker(methodInfo);
            }

            return(cam);
        }
Пример #2
0
        private static CollectionAdapterModel CreateCollectionAdapterModel()
        {
            var cam = new CollectionAdapterModel();

            var sourceElementType      = typeof(TSource).ExtractCollectionType();
            var destinationElementType = typeof(TDestinationElement);

            if (destinationElementType.IsPrimitiveRoot() || destinationElementType == typeof(object))
            {
                cam.IsPrimitive = true;

                var converter = sourceElementType.CreatePrimitiveConverter(destinationElementType);
                if (converter != null)
                {
                    cam.AdaptInvoker = converter;
                }
            }
            else if (destinationElementType.IsCollection())
            {
                var methodInfo = typeof(CollectionAdapter <, , ,>)
                                 .MakeGenericType(sourceElementType.ExtractCollectionType(), sourceElementType, destinationElementType.ExtractCollectionType(), destinationElementType)
                                 .GetMethod("Adapt", new[] { sourceElementType, typeof(bool), typeof(Dictionary <,>).MakeGenericType(typeof(long), typeof(int)) });
                cam.AdaptInvoker = FastInvoker.GetMethodInvoker(methodInfo);
            }
            else
            {
                var methodInfo = typeof(ClassAdapter <,>)
                                 .MakeGenericType(sourceElementType, destinationElementType)
                                 .GetMethod("Adapt", new[] { sourceElementType, typeof(bool), typeof(Dictionary <,>).MakeGenericType(typeof(long), typeof(int)) });

                cam.AdaptInvoker = FastInvoker.GetMethodInvoker(methodInfo);
            }

            return(cam);
        }