public bool TryCreateConverter( Type source, Type target, ChangeTypeProvider root, [NotNullWhen(true)] out ChangeType?converter) { return(_changeTypeProvider(source, target, out converter)); }
public bool TryCreateConverter( Type source, Type target, ChangeTypeProvider root, [NotNullWhen(true)] out ChangeType?converter) { Type?sourceElement = ExtendedType.Tools.GetElementType(source); Type?targetElement = ExtendedType.Tools.GetElementType(target); if (sourceElement is not null && targetElement is not 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 static IServiceCollection AddTypeConverter( this IServiceCollection services, ChangeTypeProvider changeType) { if (services is null) { throw new ArgumentNullException(nameof(services)); } return(services.AddSingleton <IChangeTypeProvider>( sp => new DelegateChangeTypeProvider(changeType))); }
public static IRequestExecutorBuilder AddTypeConverter( this IRequestExecutorBuilder builder, ChangeTypeProvider changeType) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } builder.Services.AddSingleton <IChangeTypeProvider>( sp => new DelegateChangeTypeProvider(changeType)); return(builder); }
public bool TryCreateConverter( Type source, Type target, ChangeTypeProvider root, [NotNullWhen(true)] out ChangeType?converter) { if (_source == source && _target == target) { converter = _converter; return(true); } converter = null; return(false); }
public bool TryCreateConverter( Type source, Type target, ChangeTypeProvider root, out ChangeType converter) { if (source == typeof(int) && target == typeof(string)) { converter = input => input?.ToString(); return(true); } converter = null; return(false); }
public bool TryCreateConverter( Type source, Type target, ChangeTypeProvider root, [NotNullWhen(true)] out ChangeType?converter) { Type innerFrom = GetUnderlyingNullableType(source); Type innerTo = GetUnderlyingNullableType(target); if ((innerFrom != source || innerTo != target) && root(innerFrom, innerTo, out converter)) { return(true); } converter = null; return(false); }
public bool TryCreateConverter( Type source, Type target, ChangeTypeProvider root, [NotNullWhen(true)] out ChangeType converter) { if (source == typeof(string) && target.IsEnum) { converter = input => Enum.Parse(target, (string)input, true); return(true); } if (source.IsEnum && target == typeof(string)) { converter = input => input?.ToString(); return(true); } converter = null; return(false); }
public DelegateChangeTypeProvider(ChangeTypeProvider changeTypeProvider) { _changeTypeProvider = changeTypeProvider; }