DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="2.2.0" />

 Func

public static class Func
Provides extension methods for delegate Func<T> and predefined functions.
using System; using System.Runtime.CompilerServices; namespace DotNext { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class Func { [System.Runtime.CompilerServices.NullableContext(0)] private static class Id<I, [System.Runtime.CompilerServices.Nullable(2)] O> where I : O { [System.Runtime.CompilerServices.Nullable(1)] internal static readonly Func<I, O> Value = Converter.Identity<I, O>; } [System.Runtime.CompilerServices.Nullable(0)] private static class IsNullFunc<T> where T : class { internal static readonly Func<T, bool> Value = ObjectExtensions.IsNull; } [System.Runtime.CompilerServices.Nullable(0)] private static class IsNotNullFunc<T> where T : class { internal static readonly Func<T, bool> Value = ObjectExtensions.IsNotNull; } public static Func<T, bool> IsNull<T>() where T : class { return IsNullFunc<T>.Value; } public static Func<T, bool> IsNotNull<T>() where T : class { return IsNotNullFunc<T>.Value; } public static Func<I, O> Identity<[System.Runtime.CompilerServices.Nullable(0)] I, [System.Runtime.CompilerServices.Nullable(2)] O>() where I : O { return Id<I, O>.Value; } public static Func<T, T> Identity<[System.Runtime.CompilerServices.Nullable(2)] T>() { return Identity<T, T>(); } public static Predicate<T> AsPredicate<[System.Runtime.CompilerServices.Nullable(2)] T>(this Func<T, bool> predicate) { return predicate.ChangeType<Predicate<T>>(); } public static Converter<I, O> AsConverter<[System.Runtime.CompilerServices.Nullable(2)] I, [System.Runtime.CompilerServices.Nullable(2)] O>(this Func<I, O> function) { return function.ChangeType<Converter<I, O>>(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] R>(this Func<R> function) { try { return function(); } catch (Exception error) { return new Result<R>(error); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] R>(this Func<T, R> function, T arg) { try { return function(arg); } catch (Exception error) { return new Result<R>(error); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] R>(this Func<T1, T2, R> function, T1 arg1, T2 arg2) { try { return function(arg1, arg2); } catch (Exception error) { return new Result<R>(error); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] R>(this Func<T1, T2, T3, R> function, T1 arg1, T2 arg2, T3 arg3) { try { return function(arg1, arg2, arg3); } catch (Exception error) { return new Result<R>(error); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4, [System.Runtime.CompilerServices.Nullable(2)] R>(this Func<T1, T2, T3, T4, R> function, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { try { return function(arg1, arg2, arg3, arg4); } catch (Exception error) { return new Result<R>(error); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4, [System.Runtime.CompilerServices.Nullable(2)] T5, [System.Runtime.CompilerServices.Nullable(2)] R>(this Func<T1, T2, T3, T4, T5, R> function, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { try { return function(arg1, arg2, arg3, arg4, arg5); } catch (Exception error) { return new Result<R>(error); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4, [System.Runtime.CompilerServices.Nullable(2)] T5, [System.Runtime.CompilerServices.Nullable(2)] T6, [System.Runtime.CompilerServices.Nullable(2)] R>(this Func<T1, T2, T3, T4, T5, T6, R> function, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) { try { return function(arg1, arg2, arg3, arg4, arg5, arg6); } catch (Exception error) { return new Result<R>(error); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4, [System.Runtime.CompilerServices.Nullable(2)] T5, [System.Runtime.CompilerServices.Nullable(2)] T6, [System.Runtime.CompilerServices.Nullable(2)] T7, [System.Runtime.CompilerServices.Nullable(2)] R>(this Func<T1, T2, T3, T4, T5, T6, T7, R> function, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) { try { return function(arg1, arg2, arg3, arg4, arg5, arg6, arg7); } catch (Exception error) { return new Result<R>(error); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4, [System.Runtime.CompilerServices.Nullable(2)] T5, [System.Runtime.CompilerServices.Nullable(2)] T6, [System.Runtime.CompilerServices.Nullable(2)] T7, [System.Runtime.CompilerServices.Nullable(2)] T8, [System.Runtime.CompilerServices.Nullable(2)] R>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, R> function, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) { try { return function(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); } catch (Exception error) { return new Result<R>(error); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4, [System.Runtime.CompilerServices.Nullable(2)] T5, [System.Runtime.CompilerServices.Nullable(2)] T6, [System.Runtime.CompilerServices.Nullable(2)] T7, [System.Runtime.CompilerServices.Nullable(2)] T8, [System.Runtime.CompilerServices.Nullable(2)] T9, [System.Runtime.CompilerServices.Nullable(2)] R>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, R> function, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) { try { return function(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); } catch (Exception error) { return new Result<R>(error); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<R> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4, [System.Runtime.CompilerServices.Nullable(2)] T5, [System.Runtime.CompilerServices.Nullable(2)] T6, [System.Runtime.CompilerServices.Nullable(2)] T7, [System.Runtime.CompilerServices.Nullable(2)] T8, [System.Runtime.CompilerServices.Nullable(2)] T9, [System.Runtime.CompilerServices.Nullable(2)] T10, [System.Runtime.CompilerServices.Nullable(2)] R>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R> function, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10) { try { return function(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); } catch (Exception error) { return new Result<R>(error); } } } }