-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParameterWithMarshalAsCollection.cs
315 lines (299 loc) · 14.6 KB
/
ParameterWithMarshalAsCollection.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace NativeGenericDelegates
{
/// <summary>
/// Represents a collection of native generic delegate method parameters with optional marshaling behavior.
/// </summary>
/// <remarks>
/// <para>
/// This collection does not include the return parameter (if any). See <see
/// cref="NativeGenericDelegateInfo.ReturnParameter">NativeGenericDelegateInfo.ReturnParameter</see> to access the return
/// parameter.
/// </para>
/// </remarks>
internal sealed class ParameterWithMarshalAsCollection :
IEquatable<ParameterWithMarshalAsCollection>,
IEqualityOperators<ParameterWithMarshalAsCollection, ParameterWithMarshalAsCollection, bool>,
IReadOnlyList<ParameterWithMarshalAs>
{
public static readonly ParameterWithMarshalAsCollection Empty = new();
private readonly ParameterWithMarshalAs[] parameters;
public static bool operator ==(ParameterWithMarshalAsCollection? left, ParameterWithMarshalAsCollection? right) =>
left?.Equals(right) ?? right is null;
public static bool operator !=(ParameterWithMarshalAsCollection? left, ParameterWithMarshalAsCollection? right) =>
!(left == right);
#region FromTypes generic overloads
/// <summary>
/// Creates a collection from the given generic type parameters with optional marshaling behavior.
/// </summary>
/// <param name="marshalParamAs">
/// Optionally defines the marshaling behavior for the parameters. The array length and order must match the generic type
/// parameters.
/// </param>
/// <returns>The new parameter collection.</returns>
public static ParameterWithMarshalAsCollection FromTypes<T>(MarshalAsAttribute?[]? marshalParamAs = null) =>
FromTypes(marshalParamAs, typeof(T));
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2>(MarshalAsAttribute?[]? marshalParamAs = null) =>
FromTypes(marshalParamAs, typeof(T1), typeof(T2));
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3>(MarshalAsAttribute?[]? marshalParamAs = null) =>
FromTypes(marshalParamAs, typeof(T1), typeof(T2), typeof(T3));
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4>
(
MarshalAsAttribute?[]? marshalParamAs = null
) => FromTypes(marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4));
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4, T5>
(
MarshalAsAttribute?[]? marshalParamAs = null
) => FromTypes(marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5));
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4, T5, T6>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes(marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6));
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4, T5, T6, T7>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes(marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7));
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4, T5, T6, T7, T8>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes
(
marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8)
);
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4, T5, T6, T7, T8, T9>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes
(
marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8),
typeof(T9)
);
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes
(
marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8),
typeof(T9), typeof(T10)
);
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes
(
marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8),
typeof(T9), typeof(T10), typeof(T11)
);
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes
(
marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8),
typeof(T9), typeof(T10), typeof(T11), typeof(T12)
);
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes
(
marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8),
typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13)
);
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection FromTypes<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes
(
marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8),
typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13), typeof(T14)
);
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection
FromTypes<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes
(
marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8),
typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13), typeof(T14), typeof(T15)
);
/// <inheritdoc cref="FromTypes{T}(MarshalAsAttribute?[]?)"/>
public static ParameterWithMarshalAsCollection
FromTypes<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>
(
MarshalAsAttribute?[]? marshalParamAs
) => FromTypes
(
marshalParamAs, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8),
typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13), typeof(T14), typeof(T15), typeof(T16)
);
#endregion FromTypes generic overloads
/// <summary>
/// Creates a collection from the given parameter types with optional marshaling behavior.
/// </summary>
/// <param name="marshalParamAs">
/// Optionally defines the marshaling behavior for the parameters. The array length and order must match the <paramref
/// name="parameterTypes"/> array.
/// </param>
/// <param name="parameterTypes">The parameter types.</param>
/// <returns>The new parameter collection.</returns>
/// <exception cref="ArgumentException">
/// The <paramref name="parameterTypes"/> and <paramref name="marshalParamAs"/> arrays are of different lengths.
/// </exception>
private static ParameterWithMarshalAsCollection FromTypes
(
MarshalAsAttribute?[]? marshalParamAs,
params Type[] parameterTypes
)
{
if (marshalParamAs is null)
{
marshalParamAs = new MarshalAsAttribute[parameterTypes.Length];
}
else if (marshalParamAs.Length != parameterTypes.Length)
{
throw new ArgumentException
(
$"{nameof(MarshalAsAttribute)} array must be of same length as parameter type array.",
nameof(marshalParamAs)
);
}
var parameters = new ParameterWithMarshalAs[parameterTypes.Length];
for (int i = 0; i < parameterTypes.Length; ++i)
{
parameters[i] = new(parameterTypes[i], marshalParamAs[i]);
}
return new(parameters);
}
/// <summary>
/// Creates a collection from the given parameters.
/// </summary>
/// <param name="parameters">The native generic delegate parameters with optional marshaling behavior.</param>
public ParameterWithMarshalAsCollection(params ParameterWithMarshalAs[] parameters)
{
ArgumentNullException.ThrowIfNull(parameters);
for (int i = 0; i < parameters.Length; ++i)
{
ArgumentNullException.ThrowIfNull(parameters[i]);
}
this.parameters = parameters;
}
/// <summary>
/// Creates a collection from the given method.
/// </summary>
/// <param name="method">The method to copy the signature and marshaling behavior from.</param>
/// <inheritdoc cref="INativeAction.FromFunctionPointer(nint, CallingConvention)"
/// path="//param[@name='marshalParamAs']"/>
public ParameterWithMarshalAsCollection(MethodInfo method, MarshalAsAttribute?[]? marshalParamAs = null)
{
ArgumentNullException.ThrowIfNull(method);
var parameterInfos = method.GetParameters();
parameters = parameterInfos.Length != 0 ?
new ParameterWithMarshalAs[parameterInfos.Length] :
Array.Empty<ParameterWithMarshalAs>();
if ((marshalParamAs?.Length ?? 0) != parameters.Length)
{
throw new ArgumentException
(
$"{nameof(MarshalAsAttribute)} array must be of same length as method parameter list.",
nameof(marshalParamAs)
);
}
for (int i = 0; i < parameterInfos.Length; ++i)
{
parameters[i] = new(parameterInfos[i], marshalParamAs?[i]);
}
}
/// <summary>
/// Creates a collection from the given delegate's method.
/// </summary>
/// <param name="d">The delegate to copy the method signature and marshaling behavior from.</param>
/// <inheritdoc cref="ParameterWithMarshalAsCollection(MethodInfo, MarshalAsAttribute?[]?)"
/// path="//param[@name='marshalParamAs']"/>
public ParameterWithMarshalAsCollection(Delegate d, MarshalAsAttribute?[]? marshalParamAs = null) :
this(d?.Method!, marshalParamAs)
{ }
/// <summary>
/// Gets the parameter at the given index.
/// </summary>
/// <param name="index">The index of the requested parameter.</param>
/// <returns>The requested parameter.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is less than zero or greater than or equal to <see cref="Count">Count</see>.
/// </exception>
public ParameterWithMarshalAs this[int index] => parameters[index];
/// <summary>
/// Gets the number of parameters in this collection.
/// </summary>
public int Count => parameters.Length;
public override bool Equals(object? obj)
{
return obj is ParameterWithMarshalAsCollection other && Equals(other);
}
public bool Equals(ParameterWithMarshalAsCollection? other)
{
return other is not null && parameters.SequenceEqual(other.parameters);
}
public IEnumerator<ParameterWithMarshalAs> GetEnumerator()
{
return ((IEnumerable<ParameterWithMarshalAs>)parameters).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public override int GetHashCode()
{
int hash = parameters.Length;
foreach (var parameter in parameters)
{
hash = HashCode.Combine(hash, parameter);
}
return hash;
}
public override string ToString()
{
if (parameters.Length == 0)
{
return $"{nameof(ParameterWithMarshalAsCollection)} {{ }}";
}
StringBuilder sb = new(nameof(ParameterWithMarshalAsCollection));
sb.Append($"{Environment.NewLine}{{{Environment.NewLine}");
for (int i = 0; i < parameters.Length; ++i)
{
sb.Append($" {parameters[i]}");
if ((i + 1) != parameters.Length)
{
sb.Append($",{Environment.NewLine}");
}
else
{
sb.Append(Environment.NewLine);
}
}
sb.Append('}');
return sb.ToString();
}
}
}