DelegateHelpers
Represents various extensions of delegates.
using System;
using System.Buffers;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DotNext
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class DelegateHelpers
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
private abstract class Closure
{
internal readonly MulticastDelegate Delegate;
internal abstract object Target { get; }
private protected Closure(MulticastDelegate action)
{
Delegate = action;
}
}
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class Closure<T> : Closure where T : class
{
private readonly T target;
internal override object Target => target;
private Closure(T target, MulticastDelegate action)
: base(action)
{ get; }
private void InvokeAction()
{
Unsafe.As<Action<T>>((object)Delegate)(target);
}
internal static Action Create(Action<T> action, T arg)
{
return new Closure<T>(arg, action).InvokeAction;
}
private TResult InvokeFunc<[System.Runtime.CompilerServices.Nullable(2)] TResult>()
{
return Unsafe.As<Func<T, TResult>>((object)Delegate)(this.target);
}
internal static Func<TResult> Create<[System.Runtime.CompilerServices.Nullable(2)] TResult>(Func<T, TResult> func, T arg)
{
return new Closure<T>(arg, (MulticastDelegate)func).InvokeFunc<TResult>;
}
private void InvokeAction<[System.Runtime.CompilerServices.Nullable(2)] T2>(T2 arg2)
{
Unsafe.As<Action<T, T2>>((object)Delegate)(this.target, arg2);
}
internal static Action<T2> Create<[System.Runtime.CompilerServices.Nullable(2)] T2>(Action<T, T2> action, T arg1)
{
return new Closure<T>(arg1, (MulticastDelegate)action).InvokeAction<T2>;
}
private TResult InvokeFunc<[System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] TResult>(T2 arg2)
{
return Unsafe.As<Func<T, T2, TResult>>((object)Delegate)(this.target, arg2);
}
internal static Func<T2, TResult> Create<[System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] TResult>(Func<T, T2, TResult> func, T arg)
{
return new Closure<T>(arg, (MulticastDelegate)func).InvokeFunc<T2, TResult>;
}
private void InvokeAction<[System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3>(T2 arg2, T3 arg3)
{
Unsafe.As<Action<T, T2, T3>>((object)Delegate)(this.target, arg2, arg3);
}
internal static Action<T2, T3> Create<[System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3>(Action<T, T2, T3> action, T arg1)
{
return new Closure<T>(arg1, (MulticastDelegate)action).InvokeAction<T2, T3>;
}
private TResult InvokeFunc<[System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] TResult>(T2 arg2, T3 arg3)
{
return Unsafe.As<Func<T, T2, T3, TResult>>((object)Delegate)(this.target, arg2, arg3);
}
internal static Func<T2, T3, TResult> Create<[System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] TResult>(Func<T, T2, T3, TResult> func, T arg)
{
return new Closure<T>(arg, (MulticastDelegate)func).InvokeFunc<T2, T3, TResult>;
}
private void InvokeAction<[System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4>(T2 arg2, T3 arg3, T4 arg4)
{
Unsafe.As<Action<T, T2, T3, T4>>((object)Delegate)(this.target, arg2, arg3, arg4);
}
internal static Action<T2, T3, T4> Create<[System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4>(Action<T, T2, T3, T4> action, T arg1)
{
return new Closure<T>(arg1, (MulticastDelegate)action).InvokeAction<T2, T3, T4>;
}
private TResult InvokeFunc<[System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4, [System.Runtime.CompilerServices.Nullable(2)] TResult>(T2 arg2, T3 arg3, T4 arg4)
{
return Unsafe.As<Func<T, T2, T3, T4, TResult>>((object)Delegate)(this.target, arg2, arg3, arg4);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(1)]
internal static Func<T2, T3, T4, TResult> Create<T2, T3, T4, TResult>([System.Runtime.CompilerServices.Nullable(1)] Func<T, T2, T3, T4, TResult> func, [System.Runtime.CompilerServices.Nullable(1)] T arg)
{
return new Closure<T>(arg, (MulticastDelegate)func).InvokeFunc<T2, T3, T4, TResult>;
}
private void InvokeAction<[System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4, [System.Runtime.CompilerServices.Nullable(2)] T5>(T2 arg2, T3 arg3, T4 arg4, T5 arg5)
{
Unsafe.As<Action<T, T2, T3, T4, T5>>((object)Delegate)(this.target, arg2, arg3, arg4, arg5);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(1)]
internal static Action<T2, T3, T4, T5> Create<T2, T3, T4, T5>([System.Runtime.CompilerServices.Nullable(1)] Action<T, T2, T3, T4, T5> action, [System.Runtime.CompilerServices.Nullable(1)] T arg1)
{
return new Closure<T>(arg1, (MulticastDelegate)action).InvokeAction<T2, T3, T4, T5>;
}
private TResult InvokeFunc<[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>(T2 arg2, T3 arg3, T4 arg4, T5 arg5)
{
return Unsafe.As<Func<T, T2, T3, T4, T5, TResult>>((object)Delegate)(this.target, arg2, arg3, arg4, arg5);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(1)]
internal static Func<T2, T3, T4, T5, TResult> Create<T2, T3, T4, T5, TResult>([System.Runtime.CompilerServices.Nullable(1)] Func<T, T2, T3, T4, T5, TResult> func, [System.Runtime.CompilerServices.Nullable(1)] T arg)
{
return new Closure<T>(arg, (MulticastDelegate)func).InvokeFunc<T2, T3, T4, T5, TResult>;
}
}
[StructLayout(LayoutKind.Auto)]
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
private readonly struct TargetRewriter : ISupplier<Delegate, object>
{
private readonly object target;
internal TargetRewriter(object newTarget)
{ get; }
[return: System.Runtime.CompilerServices.Nullable(2)]
object ISupplier<Delegate, object>.Invoke(Delegate d)
{ get; }
}
[StructLayout(LayoutKind.Auto)]
[System.Runtime.CompilerServices.NullableContext(0)]
private readonly struct EmptyTargetRewriter : ISupplier<Delegate, object>
{
[System.Runtime.CompilerServices.NullableContext(1)]
[return: System.Runtime.CompilerServices.Nullable(2)]
object ISupplier<Delegate, object>.Invoke(Delegate d)
{ get; }
}
[System.Runtime.CompilerServices.NullableContext(1)]
private static TOutput Bind<[System.Runtime.CompilerServices.Nullable(0)] TInput, [System.Runtime.CompilerServices.Nullable(0)] TOutput, T>(TInput d, T obj, [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1
})] IntPtr closureFactory) where TInput : MulticastDelegate where TOutput : MulticastDelegate where T : class
{
if (obj == null)
throw new ArgumentNullException("obj");
if (d.Target != null)
return (TOutput);
return d.ChangeType<TOutput, TargetRewriter>(new TargetRewriter(obj));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public unsafe static Action Bind<T>(this Action<T> action, T obj) where T : class
{
return Bind<Action<T>, Action, T>(action, obj, (IntPtr)(void*));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public unsafe static Func<TResult> Bind<T, [System.Runtime.CompilerServices.Nullable(2)] TResult>(this Func<T, TResult> func, T obj) where T : class
{
return Bind<Func<T, TResult>, Func<TResult>, T>(func, obj, (IntPtr)(void*));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public unsafe static Func<T2, TResult> Bind<T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] TResult>(this Func<T1, T2, TResult> func, T1 obj) where T1 : class
{
return Bind<Func<T1, T2, TResult>, Func<T2, TResult>, T1>(func, obj, (IntPtr)(void*));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public unsafe static Action<T2> Bind<T1, [System.Runtime.CompilerServices.Nullable(2)] T2>(this Action<T1, T2> action, T1 obj) where T1 : class
{
return Bind<Action<T1, T2>, Action<T2>, T1>(action, obj, (IntPtr)(void*));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public unsafe static Func<T2, T3, TResult> Bind<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> func, T1 obj) where T1 : class
{
return Bind<Func<T1, T2, T3, TResult>, Func<T2, T3, TResult>, T1>(func, obj, (IntPtr)(void*));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public unsafe static Action<T2, T3> Bind<T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3>(this Action<T1, T2, T3> action, T1 obj) where T1 : class
{
return Bind<Action<T1, T2, T3>, Action<T2, T3>, T1>(action, obj, (IntPtr)(void*));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public unsafe static Func<T2, T3, T4, TResult> Bind<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> func, T1 obj) where T1 : class
{
return Bind<Func<T1, T2, T3, T4, TResult>, Func<T2, T3, T4, TResult>, T1>(func, obj, (IntPtr)(void*));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public unsafe static Action<T2, T3, T4> Bind<T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4>(this Action<T1, T2, T3, T4> action, T1 obj) where T1 : class
{
return Bind<Action<T1, T2, T3, T4>, Action<T2, T3, T4>, T1>(action, obj, (IntPtr)(void*));
}
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T2, T3, T4, T5, TResult> Bind<[System.Runtime.CompilerServices.Nullable(1)] T1, T2, T3, T4, T5, TResult>([System.Runtime.CompilerServices.Nullable(1)] this Func<T1, T2, T3, T4, T5, TResult> func, [System.Runtime.CompilerServices.Nullable(1)] T1 obj) where T1 : class
{
return Bind<Func<T1, T2, T3, T4, T5, TResult>, Func<T2, T3, T4, T5, TResult>, T1>(func, obj, (IntPtr)(void*));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public unsafe static Action<T2, T3, T4, T5> Bind<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>(this Action<T1, T2, T3, T4, T5> action, T1 obj) where T1 : class
{
return Bind<Action<T1, T2, T3, T4, T5>, Action<T2, T3, T4, T5>, T1>(action, obj, (IntPtr)(void*));
}
[System.Runtime.CompilerServices.NullableContext(1)]
private static MethodInfo GetMethod<[System.Runtime.CompilerServices.Nullable(0)] TDelegate>(Expression<TDelegate> expression) where TDelegate : Delegate
{
Expression body = expression.Body;
MethodCallExpression methodCallExpression = body as MethodCallExpression;
if (methodCallExpression == null) {
MemberExpression memberExpression = body as MemberExpression;
if (memberExpression == null) {
BinaryExpression binaryExpression = body as BinaryExpression;
if (binaryExpression == null) {
IndexExpression indexExpression = body as IndexExpression;
if (indexExpression == null) {
UnaryExpression unaryExpression = body as UnaryExpression;
if (unaryExpression != null && (object)unaryExpression.Method != null)
return unaryExpression.Method;
} else if ((object)indexExpression.Indexer != null && indexExpression.Indexer.CanRead) {
return indexExpression.Indexer.GetMethod;
}
} else if ((object)binaryExpression.Method != null) {
return binaryExpression.Method;
}
} else {
PropertyInfo propertyInfo = memberExpression.Member as PropertyInfo;
if ((object)propertyInfo != null && propertyInfo.CanRead)
return propertyInfo.GetMethod;
}
throw new ArgumentException(ExceptionMessages.InvalidExpressionTree, "expression");
}
return methodCallExpression.Method;
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static TDelegate CreateOpenDelegate<[System.Runtime.CompilerServices.Nullable(0)] TDelegate>(Expression<TDelegate> expression) where TDelegate : Delegate
{
return CreateDelegate<TDelegate>(GetMethod(expression), null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static Action<T, TValue> CreateOpenDelegate<T, [System.Runtime.CompilerServices.Nullable(2)] TValue>(Expression<Func<T, TValue>> properyExpr) where T : class
{
MemberExpression memberExpression = properyExpr.Body as MemberExpression;
if (memberExpression != null) {
PropertyInfo propertyInfo = memberExpression.Member as PropertyInfo;
if ((object)propertyInfo != null && propertyInfo.CanWrite)
return CreateDelegate<Action<T, TValue>>(propertyInfo.SetMethod, null);
}
throw new ArgumentException(ExceptionMessages.InvalidExpressionTree, "properyExpr");
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static Func<object, TDelegate> CreateClosedDelegateFactory<[System.Runtime.CompilerServices.Nullable(0)] TDelegate>(Expression<TDelegate> expression) where TDelegate : Delegate
{
return GetMethod(expression).CreateDelegate<TDelegate>;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.CompilerServices.NullableContext(1)]
public static EventHandler<T> Contravariant<TBase, T>(this EventHandler<TBase> handler) where TBase : class where T : class, TBase
{
return handler.ChangeType<EventHandler<T>>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.CompilerServices.NullableContext(1)]
public static TDelegate CreateDelegate<[System.Runtime.CompilerServices.Nullable(0)] TDelegate>(this MethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] object target = null) where TDelegate : Delegate
{
return (TDelegate)method.CreateDelegate(typeof(TDelegate), target);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static TDelegate ChangeType<[System.Runtime.CompilerServices.Nullable(0)] TDelegate>(this Delegate d) where TDelegate : Delegate
{
if (!(d is TDelegate))
return d.ChangeType<TDelegate, EmptyTargetRewriter>(default(EmptyTargetRewriter));
return Unsafe.As<TDelegate>(d);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static RefAction<T, TArgs> CreateDelegate<T, TArgs>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new RefAction<T, TArgs>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static RefAction<TRef, TArgs> CreateDelegate<T, TRef, TArgs>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new RefAction<TRef, TArgs>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static RefFunc<T, TArgs, TResult> CreateDelegate<T, TArgs, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new RefFunc<T, TArgs, TResult>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static RefFunc<TRef, TArgs, TResult> CreateDelegate<T, TRef, TArgs, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new RefFunc<TRef, TArgs, TResult>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.CompilerServices.NullableContext(0)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action CreateDelegate(IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action(null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public unsafe static Action CreateDelegate<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] IntPtr ptr, T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action(obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public unsafe static Action<T> CreateDelegate<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public unsafe static Action<T1> CreateDelegate<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] T1>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] IntPtr ptr, T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public unsafe static Func<TResult> CreateDelegate<[System.Runtime.CompilerServices.Nullable(2)] TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<TResult>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public unsafe static Func<TResult> CreateDelegate<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(2)] TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] IntPtr ptr, T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<TResult>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action<T1, T2> CreateDelegate<T1, T2>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1, T2>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action<T1, T2> CreateDelegate<T, T1, T2>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1, T2>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: System.Runtime.CompilerServices.Nullable(1)]
internal unsafe static Converter<TInput, TOutput> CreateConverter<TInput, TOutput>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Converter<TInput, TOutput>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T, TResult> CreateDelegate<T, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T, TResult>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<TArg, TResult> CreateDelegate<T, TArg, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<TArg, TResult>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action<T1, T2, T3> CreateDelegate<T1, T2, T3>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1, T2, T3>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action<T1, T2, T3> CreateDelegate<T, T1, T2, T3>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1, T2, T3>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T1, T2, TResult> CreateDelegate<T1, T2, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T1, T2, TResult>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T1, T2, TResult> CreateDelegate<T, T1, T2, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T1, T2, TResult>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action<T1, T2, T3, T4> CreateDelegate<T1, T2, T3, T4>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1, T2, T3, T4>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action<T1, T2, T3, T4> CreateDelegate<T, T1, T2, T3, T4>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1, T2, T3, T4>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T1, T2, T3, TResult> CreateDelegate<T1, T2, T3, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T1, T2, T3, TResult>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T1, T2, T3, TResult> CreateDelegate<T, T1, T2, T3, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T1, T2, T3, TResult>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action<T1, T2, T3, T4, T5> CreateDelegate<T1, T2, T3, T4, T5>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1, T2, T3, T4, T5>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action<T1, T2, T3, T4, T5> CreateDelegate<T, T1, T2, T3, T4, T5>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1, T2, T3, T4, T5>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T1, T2, T3, T4, TResult> CreateDelegate<T1, T2, T3, T4, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T1, T2, T3, T4, TResult>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T1, T2, T3, T4, TResult> CreateDelegate<T, T1, T2, T3, T4, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T1, T2, T3, T4, TResult>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action<T1, T2, T3, T4, T5, T6> CreateDelegate<T1, T2, T3, T4, T5, T6>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1, T2, T3, T4, T5, T6>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Action<T1, T2, T3, T4, T5, T6> CreateDelegate<T, T1, T2, T3, T4, T5, T6>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Action<T1, T2, T3, T4, T5, T6>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T1, T2, T3, T4, T5, TResult> CreateDelegate<T1, T2, T3, T4, T5, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T1, T2, T3, T4, T5, TResult>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T1, T2, T3, T4, T5, TResult> CreateDelegate<T, T1, T2, T3, T4, T5, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T1, T2, T3, T4, T5, TResult>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T1, T2, T3, T4, T5, T6, TResult> CreateDelegate<T1, T2, T3, T4, T5, T6, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T1, T2, T3, T4, T5, T6, TResult>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static Func<T1, T2, T3, T4, T5, T6, TResult> CreateDelegate<T, T1, T2, T3, T4, T5, T6, TResult>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1,
1,
1,
1,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new Func<T1, T2, T3, T4, T5, T6, TResult>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static ReadOnlySpanAction<T, TArg> CreateDelegate<T, TArg>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
0,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new ReadOnlySpanAction<T, TArg>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static ReadOnlySpanAction<TItem, TArg> CreateDelegate<T, TItem, TArg>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
0,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new ReadOnlySpanAction<TItem, TArg>((object)obj, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static SpanAction<T, TArg> CreateDelegate<T, TArg>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
0,
1,
1
})] IntPtr ptr)
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new SpanAction<T, TArg>((object)null, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public unsafe static SpanAction<TItem, TArg> CreateDelegate<T, TItem, TArg>([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
0,
1,
1
})] IntPtr ptr, [System.Runtime.CompilerServices.Nullable(1)] T obj) where T : class
{
if (ptr == (IntPtr)(void*)null)
throw new ArgumentNullException("ptr");
return new SpanAction<TItem, TArg>((object)obj, ptr);
}
[System.Runtime.CompilerServices.NullableContext(1)]
private static T Unbind<[System.Runtime.CompilerServices.Nullable(0)] T>(this Delegate del, Type targetType) where T : MulticastDelegate
{
object target = del.Target;
Closure closure = target as Closure;
if (closure != null) {
if (ObjectExtensions.IsContravariant(closure.Target, targetType))
return closure.Delegate.ChangeType<T, EmptyTargetRewriter>(default(EmptyTargetRewriter));
} else if (ObjectExtensions.IsContravariant(target, targetType)) {
return del.ChangeType<T, TargetRewriter>(default(TargetRewriter));
}
throw new InvalidOperationException();
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static Action<T> Unbind<T>(this Action action) where T : class
{
return Unbind<Action<T>>(action, typeof(T));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static Func<T, TResult> Unbind<T, [System.Runtime.CompilerServices.Nullable(2)] TResult>(this Func<TResult> func) where T : class
{
return func.Unbind<Func<T, TResult>>(typeof(T));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static Func<T, TArg, TResult> Unbind<T, [System.Runtime.CompilerServices.Nullable(2)] TArg, [System.Runtime.CompilerServices.Nullable(2)] TResult>(this Func<TArg, TResult> func) where T : class
{
return func.Unbind<Func<T, TArg, TResult>>(typeof(T));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static Action<T, TArg> Unbind<T, [System.Runtime.CompilerServices.Nullable(2)] TArg>(this Action<TArg> action) where T : class
{
return action.Unbind<Action<T, TArg>>(typeof(T));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static Func<T, T1, T2, TResult> Unbind<T, [System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] TResult>(this Func<T1, T2, TResult> func) where T : class
{
return func.Unbind<Func<T, T1, T2, TResult>>(typeof(T));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static Action<T, T1, T2> Unbind<T, [System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2>(this Action<T1, T2> action) where T : class
{
return action.Unbind<Action<T, T1, T2>>(typeof(T));
}
[return: System.Runtime.CompilerServices.Nullable(1)]
public static Func<T, T1, T2, T3, TResult> Unbind<[System.Runtime.CompilerServices.Nullable(1)] T, T1, T2, T3, TResult>([System.Runtime.CompilerServices.Nullable(1)] this Func<T1, T2, T3, TResult> func) where T : class
{
return func.Unbind<Func<T, T1, T2, T3, TResult>>(typeof(T));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static Action<T, T1, T2, T3> Unbind<T, [System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3>(this Action<T1, T2, T3> action) where T : class
{
return action.Unbind<Action<T, T1, T2, T3>>(typeof(T));
}
[return: System.Runtime.CompilerServices.Nullable(1)]
public static Func<T, T1, T2, T3, T4, TResult> Unbind<[System.Runtime.CompilerServices.Nullable(1)] T, T1, T2, T3, T4, TResult>([System.Runtime.CompilerServices.Nullable(1)] this Func<T1, T2, T3, T4, TResult> func) where T : class
{
return func.Unbind<Func<T, T1, T2, T3, T4, TResult>>(typeof(T));
}
[return: System.Runtime.CompilerServices.Nullable(1)]
public static Action<T, T1, T2, T3, T4> Unbind<[System.Runtime.CompilerServices.Nullable(1)] T, T1, T2, T3, T4>([System.Runtime.CompilerServices.Nullable(1)] this Action<T1, T2, T3, T4> action) where T : class
{
return action.Unbind<Action<T, T1, T2, T3, T4>>(typeof(T));
}
[return: System.Runtime.CompilerServices.Nullable(1)]
public static Func<T, T1, T2, T3, T4, T5, TResult> Unbind<[System.Runtime.CompilerServices.Nullable(1)] T, T1, T2, T3, T4, T5, TResult>([System.Runtime.CompilerServices.Nullable(1)] this Func<T1, T2, T3, T4, T5, TResult> func) where T : class
{
return func.Unbind<Func<T, T1, T2, T3, T4, T5, TResult>>(typeof(T));
}
[return: System.Runtime.CompilerServices.Nullable(1)]
public static Action<T, T1, T2, T3, T4, T5> Unbind<[System.Runtime.CompilerServices.Nullable(1)] T, T1, T2, T3, T4, T5>([System.Runtime.CompilerServices.Nullable(1)] this Action<T1, T2, T3, T4, T5> action) where T : class
{
return action.Unbind<Action<T, T1, T2, T3, T4, T5>>(typeof(T));
}
[System.Runtime.CompilerServices.NullableContext(0)]
[return: System.Runtime.CompilerServices.Nullable(1)]
private static TDelegate ChangeType<TDelegate, TRewriter>([System.Runtime.CompilerServices.Nullable(1)] this Delegate d, TRewriter rewriter) where TDelegate : Delegate where TRewriter : struct, ISupplier<Delegate, object>
{
Delegate[] invocationList = d.GetInvocationList();
if (invocationList.LongLength == 1) {
if ((object)invocationList[0] != d)
return invocationList[0].ChangeType<TDelegate, TRewriter>(rewriter);
return CreateDelegate<TDelegate>(d.Method, ((ISupplier<Delegate, object>)rewriter).Invoke(d));
}
Type typeFromHandle = typeof(TDelegate);
Span<Delegate> span = invocationList.AsSpan();
for (int i = 0; i < span.Length; i++) {
ref Delegate reference = ref span[i];
reference = reference.Method.CreateDelegate(typeFromHandle, ((ISupplier<Delegate, object>)rewriter).Invoke(reference));
}
return (TDelegate)Delegate.Combine(invocationList);
}
}
}