This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
forked from dotnet/dotNext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DelegateHelpers.Bind.cs
164 lines (153 loc) · 11.4 KB
/
DelegateHelpers.Bind.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
namespace DotNext
{
public static partial class DelegateHelpers
{
private static unsafe TOutput Bind<TInput, TOutput, T>(TInput d, T obj, delegate*<TInput, T, TOutput> closureFactory)
where TInput : MulticastDelegate
where TOutput : MulticastDelegate
where T : class
{
if (obj is null)
throw new ArgumentNullException(nameof(obj));
return d.Target is null ?
ChangeType<TOutput, TargetRewriter>(d, new TargetRewriter(obj)) :
closureFactory(d, obj);
}
/// <summary>
/// Produces delegate which first parameter is implicitly bound to the given object.
/// </summary>
/// <typeparam name="T">The type of the first parameter to bind.</typeparam>
/// <param name="action">The action to bind.</param>
/// <param name="obj">The object to be passed implicitly as the first argument into the method represented by this pointer. Cannot be <see langword="null"/>.</param>
/// <returns>The delegate targeting the specified object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception>
public static unsafe Action Bind<T>(this Action<T> action, T obj)
where T : class
=> Bind<Action<T>, Action, T>(action, obj, &Closure<T>.Create);
/// <summary>
/// Produces delegate which first parameter is implicitly bound to the given object.
/// </summary>
/// <typeparam name="T">The type of the first parameter to bind.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that the delegate encapsulates.</typeparam>
/// <param name="func">The action to bind.</param>
/// <param name="obj">The object to be passed implicitly as the first argument into the method represented by this pointer. Cannot be <see langword="null"/>.</param>
/// <returns>The delegate targeting the specified object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception>
public static unsafe Func<TResult> Bind<T, TResult>(this Func<T, TResult> func, T obj)
where T : class
=> Bind<Func<T, TResult>, Func<TResult>, T>(func, obj, &Closure<T>.Create);
/// <summary>
/// Produces delegate which first parameter is implicitly bound to the given object.
/// </summary>
/// <typeparam name="T1">The type of the first parameter to bind.</typeparam>
/// <typeparam name="T2">The type of the second parameter.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that the delegate encapsulates.</typeparam>
/// <param name="func">The action to bind.</param>
/// <param name="obj">The object to be passed implicitly as the first argument into the method represented by this pointer. Cannot be <see langword="null"/>.</param>
/// <returns>The delegate targeting the specified object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception>
public static unsafe Func<T2, TResult> Bind<T1, T2, TResult>(this Func<T1, T2, TResult> func, T1 obj)
where T1 : class
=> Bind<Func<T1, T2, TResult>, Func<T2, TResult>, T1>(func, obj, &Closure<T1>.Create);
/// <summary>
/// Produces delegate which first parameter is implicitly bound to the given object.
/// </summary>
/// <typeparam name="T1">The type of the first parameter to bind.</typeparam>
/// <typeparam name="T2">The type of the second parameter.</typeparam>
/// <param name="action">The action to bind.</param>
/// <param name="obj">The object to be passed implicitly as the first argument into the method represented by this pointer. Cannot be <see langword="null"/>.</param>
/// <returns>The delegate targeting the specified object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception>
public static unsafe Action<T2> Bind<T1, T2>(this Action<T1, T2> action, T1 obj)
where T1 : class
=> Bind<Action<T1, T2>, Action<T2>, T1>(action, obj, &Closure<T1>.Create);
/// <summary>
/// Produces delegate which first parameter is implicitly bound to the given object.
/// </summary>
/// <typeparam name="T1">The type of the first parameter to bind.</typeparam>
/// <typeparam name="T2">The type of the second parameter.</typeparam>
/// <typeparam name="T3">The type of the third parameter.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that the delegate encapsulates.</typeparam>
/// <param name="func">The action to bind.</param>
/// <param name="obj">The object to be passed implicitly as the first argument into the method represented by this pointer. Cannot be <see langword="null"/>.</param>
/// <returns>The delegate targeting the specified object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception>
public static unsafe Func<T2, T3, TResult> Bind<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> func, T1 obj)
where T1 : class
=> Bind<Func<T1, T2, T3, TResult>, Func<T2, T3, TResult>, T1>(func, obj, &Closure<T1>.Create);
/// <summary>
/// Produces delegate which first parameter is implicitly bound to the given object.
/// </summary>
/// <typeparam name="T1">The type of the first parameter to bind.</typeparam>
/// <typeparam name="T2">The type of the second parameter.</typeparam>
/// <typeparam name="T3">The type of the third parameter.</typeparam>
/// <param name="action">The action to bind.</param>
/// <param name="obj">The object to be passed implicitly as the first argument into the method represented by this pointer. Cannot be <see langword="null"/>.</param>
/// <returns>The delegate targeting the specified object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception>
public static unsafe Action<T2, T3> Bind<T1, T2, T3>(this Action<T1, T2, T3> action, T1 obj)
where T1 : class
=> Bind<Action<T1, T2, T3>, Action<T2, T3>, T1>(action, obj, &Closure<T1>.Create);
/// <summary>
/// Produces delegate which first parameter is implicitly bound to the given object.
/// </summary>
/// <typeparam name="T1">The type of the first parameter to bind.</typeparam>
/// <typeparam name="T2">The type of the second parameter.</typeparam>
/// <typeparam name="T3">The type of the third parameter.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that the delegate encapsulates.</typeparam>
/// <param name="func">The action to bind.</param>
/// <param name="obj">The object to be passed implicitly as the first argument into the method represented by this pointer. Cannot be <see langword="null"/>.</param>
/// <returns>The delegate targeting the specified object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception>
public static unsafe Func<T2, T3, T4, TResult> Bind<T1, T2, T3, T4, TResult>(this Func<T1, T2, T3, T4, TResult> func, T1 obj)
where T1 : class
=> Bind<Func<T1, T2, T3, T4, TResult>, Func<T2, T3, T4, TResult>, T1>(func, obj, &Closure<T1>.Create);
/// <summary>
/// Produces delegate which first parameter is implicitly bound to the given object.
/// </summary>
/// <typeparam name="T1">The type of the first parameter to bind.</typeparam>
/// <typeparam name="T2">The type of the second parameter.</typeparam>
/// <typeparam name="T3">The type of the third parameter.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter.</typeparam>
/// <param name="action">The action to bind.</param>
/// <param name="obj">The object to be passed implicitly as the first argument into the method represented by this pointer. Cannot be <see langword="null"/>.</param>
/// <returns>The delegate targeting the specified object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception>
public static unsafe Action<T2, T3, T4> Bind<T1, T2, T3, T4>(this Action<T1, T2, T3, T4> action, T1 obj)
where T1 : class
=> Bind<Action<T1, T2, T3, T4>, Action<T2, T3, T4>, T1>(action, obj, &Closure<T1>.Create);
/// <summary>
/// Produces delegate which first parameter is implicitly bound to the given object.
/// </summary>
/// <typeparam name="T1">The type of the first parameter to bind.</typeparam>
/// <typeparam name="T2">The type of the second parameter.</typeparam>
/// <typeparam name="T3">The type of the third parameter.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter.</typeparam>
/// <typeparam name="T5">The type of the fifth parameter.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that the delegate encapsulates.</typeparam>
/// <param name="func">The action to bind.</param>
/// <param name="obj">The object to be passed implicitly as the first argument into the method represented by this pointer. Cannot be <see langword="null"/>.</param>
/// <returns>The delegate targeting the specified object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception>
public static unsafe Func<T2, T3, T4, T5, TResult> Bind<T1, T2, T3, T4, T5, TResult>(this Func<T1, T2, T3, T4, T5, TResult> func, T1 obj)
where T1 : class
=> Bind<Func<T1, T2, T3, T4, T5, TResult>, Func<T2, T3, T4, T5, TResult>, T1>(func, obj, &Closure<T1>.Create);
/// <summary>
/// Produces delegate which first parameter is implicitly bound to the given object.
/// </summary>
/// <typeparam name="T1">The type of the first parameter to bind.</typeparam>
/// <typeparam name="T2">The type of the second parameter.</typeparam>
/// <typeparam name="T3">The type of the third parameter.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter.</typeparam>
/// <typeparam name="T5">The type of the fifth parameter.</typeparam>
/// <param name="action">The action to bind.</param>
/// <param name="obj">The object to be passed implicitly as the first argument into the method represented by this pointer. Cannot be <see langword="null"/>.</param>
/// <returns>The delegate targeting the specified object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="obj"/> is <see langword="null"/>.</exception>
public static unsafe Action<T2, T3, T4, T5> Bind<T1, T2, T3, T4, T5>(this Action<T1, T2, T3, T4, T5> action, T1 obj)
where T1 : class
=> Bind<Action<T1, T2, T3, T4, T5>, Action<T2, T3, T4, T5>, T1>(action, obj, &Closure<T1>.Create);
}
}