Func
Provides extension methods for delegate Func<T> and
predefined functions.
using System;
using System.Runtime.CompilerServices;
namespace DotNext
{
[NullableContext(1)]
[Nullable(0)]
public static class Func
{
private sealed class BoxedConstant<T> : StrongBox<T>
{
internal T GetValue()
{
return Value;
}
public override string ToString()
{
ref T reference = ref Value;
T val = default(T);
if (val == null) {
val = reference;
ref reference = ref val;
if (val == null)
return null;
}
return reference.ToString();
}
}
[NullableContext(2)]
[return: Nullable(new byte[] {
1,
2
})]
public static Func<object, bool> IsTypeOf<T>()
{
return BasicExtensions.IsTypeOf<T>;
}
public static Func<T, bool> IsNull<[Nullable(2)] T>() where T : class
{
return BasicExtensions.IsNull;
}
public static Func<T, bool> IsNotNull<[Nullable(2)] T>() where T : class
{
return BasicExtensions.IsNotNull;
}
public static Func<TInput, TOutput> Identity<[Nullable(0)] TInput, [Nullable(2)] TOutput>() where TInput : TOutput
{
return BasicExtensions.Identity<TInput, TOutput>;
}
public static Func<T, T> Identity<[Nullable(2)] T>()
{
return Identity<T, T>();
}
public static Func<T, T> Identity<[Nullable(2)] T>(this Action<T> action)
{
return action.Identity<T>;
}
private static T Identity<T>(this Action<T> action, T item)
{
action(item);
return item;
}
public static Func<T> Constant<[Nullable(2)] T>(T obj)
{
if (typeof(T) == typeof(bool))
return Unsafe.As<Func<T>>((object)Constant(Unsafe.As<T, bool>(ref obj)));
if (obj != null) {
if (!typeof(T).IsValueType)
return Unsafe.As<T, object>(ref obj).UnboxRefType<T>;
return new BoxedConstant<T> {
Value = obj
}.GetValue;
}
return () => default(T);
}
private static T UnboxRefType<T>(this object obj)
{
return Unsafe.As<object, T>(ref obj);
}
private static Func<bool> Constant(bool value)
{
object obj;
if (!value) {
obj = <>O.<1>__False;
if (obj == null)
return <>O.<1>__False = (() => false);
} else
obj = (Func<bool>)(() => true);
return (Func<bool>)obj;
}
public static Predicate<T> AsPredicate<[Nullable(2)] T>(this Func<T, bool> predicate)
{
return predicate.ChangeType<Predicate<T>>();
}
public static Converter<TInput, TOutput> AsConverter<[Nullable(2)] TInput, [Nullable(2)] TOutput>(this Func<TInput, TOutput> function)
{
return function.ChangeType<Converter<TInput, TOutput>>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] TResult>(this Func<TResult> function)
{
try {
return function();
} catch (Exception error) {
return new Result<TResult>(error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] T, [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: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] T1, [Nullable(2)] T2, [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: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3, [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: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3, [Nullable(2)] T4, [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: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3, [Nullable(2)] T4, [Nullable(2)] T5, [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: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3, [Nullable(2)] T4, [Nullable(2)] T5, [Nullable(2)] T6, [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: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3, [Nullable(2)] T4, [Nullable(2)] T5, [Nullable(2)] T6, [Nullable(2)] T7, [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: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3, [Nullable(2)] T4, [Nullable(2)] T5, [Nullable(2)] T6, [Nullable(2)] T7, [Nullable(2)] T8, [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: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3, [Nullable(2)] T4, [Nullable(2)] T5, [Nullable(2)] T6, [Nullable(2)] T7, [Nullable(2)] T8, [Nullable(2)] T9, [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: Nullable(new byte[] {
0,
1
})]
public static Result<TResult> TryInvoke<[Nullable(2)] T1, [Nullable(2)] T2, [Nullable(2)] T3, [Nullable(2)] T4, [Nullable(2)] T5, [Nullable(2)] T6, [Nullable(2)] T7, [Nullable(2)] T8, [Nullable(2)] T9, [Nullable(2)] T10, [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);
}
}
}
}