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
{
private static class Id<TInput, TOutput> where TInput : TOutput
{
internal static readonly Func<TInput, TOutput> Value = Converter.Identity<TInput, TOutput>;
}
private static class IsNullFunc<T> where T : class
{
internal static readonly Func<T, bool> Value = ObjectExtensions.IsNull;
}
private static class IsNotNullFunc<T> where T : class
{
internal static readonly Func<T, bool> Value = ObjectExtensions.IsNotNull;
}
private static class TypeChecker<T>
{
internal static readonly Func<object, bool> Value = ObjectExtensions.IsTypeOf<T>;
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static Func<object, bool> IsTypeOf<T>()
{
return TypeChecker<T>.Value;
}
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<TInput, TOutput> Identity<[System.Runtime.CompilerServices.Nullable(0)] TInput, [System.Runtime.CompilerServices.Nullable(2)] TOutput>() where TInput : TOutput
{
return Id<TInput, TOutput>.Value;
}
public static Func<T, T> Identity<[System.Runtime.CompilerServices.Nullable(2)] T>()
{
return Identity<T, T>();
}
private static T Convert<T>(this object obj)
{
return (T)obj;
}
public static Func<T> Constant<[System.Runtime.CompilerServices.Nullable(2)] T>(T obj)
{
return ((object)obj).Convert<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<TInput, TOutput> AsConverter<[System.Runtime.CompilerServices.Nullable(2)] TInput, [System.Runtime.CompilerServices.Nullable(2)] TOutput>(this Func<TInput, TOutput> function)
{
return function.ChangeType<Converter<TInput, TOutput>>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] TResult>(this Func<TResult> function)
{
try {
return function();
} catch (Exception error) {
return new Result<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] TResult>(this Func<T, TResult> function, T arg)
{
try {
return function(arg);
} catch (Exception error) {
return new Result<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] TResult>(this Func<T1, T2, TResult> function, T1 arg1, T2 arg2)
{
try {
return function(arg1, arg2);
} catch (Exception error) {
return new Result<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] TResult>(this Func<T1, T2, T3, TResult> function, T1 arg1, T2 arg2, T3 arg3)
{
try {
return function(arg1, arg2, arg3);
} catch (Exception error) {
return new Result<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> 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)] TResult>(this Func<T1, T2, T3, T4, TResult> function, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
try {
return function(arg1, arg2, arg3, arg4);
} catch (Exception error) {
return new Result<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> 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)] TResult>(this Func<T1, T2, T3, T4, T5, TResult> 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<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> 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)] TResult>(this Func<T1, T2, T3, T4, T5, T6, TResult> 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<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> 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)] TResult>(this Func<T1, T2, T3, T4, T5, T6, T7, TResult> 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<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> 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)] TResult>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> 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<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> 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)] TResult>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> 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<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Result<TResult> 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)] TResult>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> 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<TResult>(error);
}
}
}
}