protected override void VisitList(
            IList <object> list,
            ConverterContext context)
        {
            Type elementType = DotNetTypeInfoFactory
                               .GetInnerListType(context.ClrType);

            if (elementType != null)
            {
                Type  listType = typeof(List <>).MakeGenericType(elementType);
                IList temp     = (IList)Activator.CreateInstance(listType);

                for (int i = 0; i < list.Count; i++)
                {
                    var valueContext = new ConverterContext();
                    valueContext.ClrType = elementType;
                    Visit(list[i], valueContext);

                    temp.Add(valueContext.Object);
                }

                context.Object = context.ClrType.IsAssignableFrom(listType)
                    ? temp
                    : _converter.Convert(listType, context.ClrType, temp);
            }
        }
Пример #2
0
        private bool TryCreateListTypeConverter(Type from, Type to,
                                                out ChangeType listConverter)
        {
            Type fromElement = DotNetTypeInfoFactory.GetInnerListType(from);
            Type toElement   = DotNetTypeInfoFactory.GetInnerListType(to);

            if (fromElement != null && toElement != null &&
                TryGetOrCreateConverter(fromElement, toElement,
                                        out ChangeType converter))
            {
                if (to.IsArray)
                {
                    listConverter = source => GenericArrayConverter(
                        toElement, (ICollection)source, converter);
                    Register(from, to, listConverter);
                }
                else
                {
                    Type listType = to.IsInterface
                        ? typeof(List <>).MakeGenericType(toElement)
                        : to;
                    listConverter = source => GenericListConverter(
                        listType, (ICollection)source, converter);
                    Register(from, to, listConverter);
                }

                return(true);
            }

            listConverter = null;
            return(false);
        }
        public void Unwrap(Type type, Type expectedReducedType)
        {
            // arrange
            // act
            Type reducedType = DotNetTypeInfoFactory.Unwrap(type);

            // assert
            Assert.Equal(expectedReducedType, reducedType);
        }
Пример #4
0
        public bool TryCreateConverter(
            Type source,
            Type target,
            ChangeTypeProvider root,
            [NotNullWhen(true)] out ChangeType?converter)
        {
            Type sourceElement = DotNetTypeInfoFactory.GetInnerListType(source);
            Type targetElement = DotNetTypeInfoFactory.GetInnerListType(target);

            if (sourceElement != null &&
                targetElement != null &&
                root(sourceElement, targetElement, out ChangeType? elementConverter))
            {
                if (target.IsArray)
                {
                    converter = input => GenericArrayConverter(
                        (ICollection?)input, targetElement, elementConverter);
                    return(true);
                }

                if (target.IsGenericType &&
                    target.GetGenericTypeDefinition() == typeof(Dictionary <,>))
                {
                    MethodInfo converterMethod =
                        _dictionaryConvert.MakeGenericMethod(targetElement.GetGenericArguments());
                    converter = source => converterMethod.Invoke(
                        null, new[] { source, elementConverter });
                    return(true);
                }

                if (target.IsGenericType &&
                    target.IsInterface)
                {
                    Type listType = typeof(List <>).MakeGenericType(targetElement);
                    if (target.IsAssignableFrom(listType))
                    {
                        converter = source => GenericListConverter(
                            (ICollection?)source, listType, elementConverter);
                        return(true);
                    }
                }

                if (target.IsGenericType &&
                    target.IsClass &&
                    typeof(ICollection).IsAssignableFrom(target))
                {
                    converter = source => GenericListConverter(
                        (ICollection?)source, target, elementConverter);
                    return(true);
                }
            }

            converter = null;
            return(false);
        }
        public void NotSupportedCases(Type clrType)
        {
            // arrange
            var factory = new DotNetTypeInfoFactory();

            // act
            bool success = factory.TryCreate(clrType, out TypeInfo typeInfo);

            // assert
            Assert.False(success);
        }
        public void CreateTypeInfoFromValueType(Type nativeType, string expectedTypeName)
        {
            // arrange
            DotNetTypeInfoFactory factory = new DotNetTypeInfoFactory();

            // act
            bool success = factory.TryCreate(nativeType, out TypeInfo typeInfo);

            // assert
            Assert.True(success);
            Assert.Equal(expectedTypeName, typeInfo.TypeFactory(new IntType()).Visualize());
        }
        public void MixedTypes(Type clrType, string expectedTypeName)
        {
            // arrange
            var factory = new DotNetTypeInfoFactory();

            // act
            bool success = factory.TryCreate(clrType, out TypeInfo typeInfo);

            // assert
            Assert.True(success);
            Assert.Equal(expectedTypeName,
                         typeInfo.TypeFactory(new StringType()).Visualize());
        }
        public void Rewrite(
            Type type,
            bool isNonNullType,
            bool isElementNonNullType,
            Type expectedReducedType)
        {
            // arrange
            // act
            Type reducedType = DotNetTypeInfoFactory.Rewrite(
                type, isNonNullType, isElementNonNullType);

            // assert
            Assert.Equal(expectedReducedType, reducedType);
        }
Пример #9
0
        private bool TryCreateListTypeConverter(Type from, Type to,
                                                out ChangeType listConverter)
        {
            Type fromElement = DotNetTypeInfoFactory.GetInnerListType(from);
            Type toElement   = DotNetTypeInfoFactory.GetInnerListType(to);

            if (fromElement != null && toElement != null &&
                TryGetOrCreateConverter(fromElement, toElement,
                                        out ChangeType converter))
            {
                if (to.IsArray)
                {
                    listConverter = source => GenericArrayConverter(
                        toElement, (ICollection)source, converter);
                    Register(from, to, listConverter);
                }
                else if (to.IsGenericType &&
                         to.GetGenericTypeDefinition() == typeof(Dictionary <,>))
                {
                    MethodInfo converterMethod =
                        _dictionaryConvert.MakeGenericMethod(toElement.GetGenericArguments());
                    listConverter = source => converterMethod.Invoke(
                        null, new[] { source, converter });
                    Register(from, to, listConverter);
                }
                else
                {
                    Type listType = to.IsInterface
                        ? typeof(List <>).MakeGenericType(toElement)
                        : to;
                    listConverter = source => GenericListConverter(
                        listType, (ICollection)source, converter);
                    Register(from, to, listConverter);
                }

                return(true);
            }

            listConverter = null;
            return(false);
        }
        public void Create_TypeInfo_From_RewrittenType()
        {
            // arrange
            Type type    = typeof(ListType <NonNullType <NativeType <string> > >);
            var  factory = new DotNetTypeInfoFactory();

            // act
            bool success = factory.TryCreate(type, out TypeInfo typeInfo);

            // assert
            Assert.True(success);

            Assert.Collection(typeInfo.Components,
                              t => Assert.Equal(typeof(ListType <NonNullType <NativeType <string> > >), t),
                              t => Assert.Equal(typeof(NonNullType <NativeType <string> >), t),
                              t => Assert.Equal(typeof(string), t));

            IType schemaType = typeInfo.TypeFactory(new StringType());

            Assert.IsType <StringType>(
                Assert.IsType <NonNullType>(
                    Assert.IsType <ListType>(schemaType).ElementType).Type);
        }