DotNext by Roman Sakno

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

 ValueFunc<T1, T2, T3, T4, T5, R>

public struct ValueFunc<T1, T2, T3, T4, T5, R> : ICallable<Func<T1, T2, T3, T4, T5, R>>, ICallable, IEquatable<ValueFunc<T1, T2, T3, T4, T5, R>>
Represents a pointer to the method with five parameters and return type.
using System; using System.ComponentModel; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace DotNext { [StructLayout(LayoutKind.Auto)] public readonly struct ValueFunc<T1, T2, T3, T4, T5, R> : ICallable<Func<T1, T2, T3, T4, T5, R>>, ICallable, IEquatable<ValueFunc<T1, T2, T3, T4, T5, R>> { private readonly IntPtr methodPtr; private readonly Func<T1, T2, T3, T4, T5, R> func; public bool IsEmpty { get { if (func == null) return methodPtr == (IntPtr)0; return false; } } public object Target => func?.Target; public ValueFunc(MethodInfo method) { this = new ValueFunc<T1, T2, T3, T4, T5, R>(DelegateHelpers.CreateDelegate<Func<T1, T2, T3, T4, T5, R>>(method, (object)null), false); } public ValueFunc(Func<T1, T2, T3, T4, T5, R> func, bool wrap = false) { if (func == null) throw new ArgumentNullException("func"); if (wrap || DelegateHelpers.IsRegularDelegate(func)) { this.func = func; methodPtr = default(IntPtr); } else { this.func = null; methodPtr = func.Method.MethodHandle.GetFunctionPointer(); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] [EditorBrowsable(EditorBrowsableState.Never)] [CLSCompliant(false)] public ValueFunc(IntPtr methodPtr) { func = null; this.methodPtr = methodPtr; } public Func<T1, T2, T3, T4, T5, R> ToDelegate() { if (methodPtr != (IntPtr)0) return new Func<T1, T2, T3, T4, T5, R>(null, methodPtr); return func; } public R Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { if (methodPtr != (IntPtr)0) return (R); return func(arg1, arg2, arg3, arg4, arg5); } object ICallable.DynamicInvoke(params object[] args) { return Invoke((T1)args[0], (T2)args[1], (T3)args[2], (T4)args[3], (T5)args[4]); } public static explicit operator Func<T1, T2, T3, T4, T5, R>([In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<T1, T2, T3, T4, T5, R> pointer) { return pointer.ToDelegate(); } public override int GetHashCode() { return func?.GetHashCode() ?? methodPtr.GetHashCode(); } public bool Equals(ValueFunc<T1, T2, T3, T4, T5, R> other) { if (methodPtr == other.methodPtr) return object.Equals(func, other.func); return false; } public override bool Equals(object other) { if (other is ValueFunc<T1, T2, T3, T4, T5, R>) { ValueFunc<T1, T2, T3, T4, T5, R> other2 = (ValueFunc<T1, T2, T3, T4, T5, R>)other; return Equals(other2); } return false; } public override string ToString() { return func?.ToString() ?? methodPtr.ToString("X"); } public static bool operator ==([In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<T1, T2, T3, T4, T5, R> first, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<T1, T2, T3, T4, T5, R> second) { if (first.methodPtr == second.methodPtr) return object.Equals(first.func, second.func); return false; } public static bool operator !=([In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<T1, T2, T3, T4, T5, R> first, [In] [System.Runtime.CompilerServices.IsReadOnly] ref ValueFunc<T1, T2, T3, T4, T5, R> second) { if (!(first.methodPtr != second.methodPtr)) return !object.Equals(first.func, second.func); return true; } } }