Converter
Provides extension methods for delegate Converter<T, U> and
predefined converters.
using System;
using System.Runtime.CompilerServices;
namespace DotNext
{
[NullableContext(1)]
[Nullable(0)]
public static class Converter
{
public static Converter<TInput, TOutput> Identity<[Nullable(0)] TInput, [Nullable(2)] TOutput>() where TInput : TOutput
{
return BasicExtensions.Identity<TInput, TOutput>;
}
public static Converter<T, T> Identity<[Nullable(2)] T>()
{
return Identity<T, T>();
}
public static Func<TInput, TOutput> AsFunc<[Nullable(2)] TInput, [Nullable(2)] TOutput>(this Converter<TInput, TOutput> converter)
{
return converter.ChangeType<Func<TInput, TOutput>>();
}
public static Predicate<T> AsPredicate<[Nullable(2)] T>(this Converter<T, bool> converter)
{
return converter.ChangeType<Predicate<T>>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: Nullable(new byte[] {
0,
1
})]
public static Result<TOutput> TryInvoke<[Nullable(2)] TInput, [Nullable(2)] TOutput>(this Converter<TInput, TOutput> converter, TInput input)
{
try {
return converter(input);
} catch (Exception error) {
return new Result<TOutput>(error);
}
}
}
}