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); } }
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 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); }
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); }