-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelegateFactory.cs
More file actions
225 lines (218 loc) · 13.3 KB
/
DelegateFactory.cs
File metadata and controls
225 lines (218 loc) · 13.3 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
namespace NativeGenericDelegates
{
/// <summary>
/// Factory methods for building a runtime native generic delegate.
/// </summary>
internal static class DelegateFactory
{
private const MethodAttributes ctorAttrs =
MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public;
private const MethodImplAttributes implAttrs = MethodImplAttributes.Runtime | MethodImplAttributes.Managed;
private const MethodAttributes invokeAttrs =
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual;
private static readonly Type[] ctorSig = new[] { typeof(object), typeof(nint) };
private static readonly AssemblyName assemblyName = new("RuntimeNativeGenericDelegates");
private static readonly AssemblyBuilder assemblyBuilder =
AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndCollect);
private static readonly ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name!);
private static readonly ConstructorInfo unmanagedFuncPtrCtorInfo =
typeof(UnmanagedFunctionPointerAttribute).GetConstructor(new[] { typeof(CallingConvention) })!;
private static readonly CustomAttributeBuilder cdeclAttrBuilder =
new(unmanagedFuncPtrCtorInfo, new object[] { CallingConvention.Cdecl });
private static readonly CustomAttributeBuilder stdCallAttrBuilder =
new(unmanagedFuncPtrCtorInfo, new object[] { CallingConvention.StdCall });
private static readonly CustomAttributeBuilder thisCallAttrBuilder =
new(unmanagedFuncPtrCtorInfo, new object[] { CallingConvention.ThisCall });
private static readonly CustomAttributeBuilder winapiAttrBuilder =
new(unmanagedFuncPtrCtorInfo, new object[] { CallingConvention.Winapi });
// cache defined runtime delegate types based on signature and marshaling behavior
private static readonly Dictionary<NativeGenericDelegateInfo, Type> delegates = new();
/// <summary>
/// Defines a runtime delegate type.
/// </summary>
/// <remarks>
/// This runtime delegate type will implement exactly one of the <see cref="INativeAction">INativeAction</see> or
/// <see cref="INativeFunc{TResult}">INativeFunc</see> family of interfaces that has a matching signature.
/// </remarks>
/// <param name="info">The native generic delegate info describing the runtime delegate.</param>
/// <returns>The <see cref="Type">Type</see> of the runtime delegate.</returns>
private static Type DefineDelegateType(NativeGenericDelegateInfo info)
{
Type returnType = info.ReturnType;
MarshalAsAttribute? marshalReturnAs = info.ReturnParameter?.MarshalAs;
Type[] parameters = info.Parameters.Select(p => p.ParameterType).ToArray();
MarshalAsAttribute?[] marshalParamAs = info.Parameters.Select(p => p.MarshalAs).ToArray();
// generate a unique name for the delegate type
string name = $"NativeGenericDelegate{Guid.NewGuid()}";
// define the type and constructor
TypeBuilder typeBuilder =
moduleBuilder.DefineType(name, TypeAttributes.Public | TypeAttributes.Sealed, typeof(MulticastDelegate));
typeBuilder.DefineConstructor(ctorAttrs, CallingConventions.Standard, ctorSig).SetImplementationFlags(implAttrs);
// implement the INativeAction/INativeFunc interface with the matching signature
typeBuilder.AddInterfaceImplementation(GetInterfaceType(returnType, parameters));
// set the unmanaged calling convention
typeBuilder.SetCustomAttribute(info.CallingConvention switch
{
CallingConvention.Cdecl => cdeclAttrBuilder,
CallingConvention.StdCall => stdCallAttrBuilder,
CallingConvention.ThisCall => thisCallAttrBuilder,
CallingConvention.Winapi => winapiAttrBuilder,
// CallingConvention.FastCall or unexpected values are unsupported
_ => throw new NotSupportedException($"Calling convention {info.CallingConvention} is not supported.")
});
// define the Invoke method
MethodBuilder methodBuilder = typeBuilder.DefineMethod("Invoke", invokeAttrs, returnType, parameters);
methodBuilder.SetImplementationFlags(implAttrs);
// define custom marshaling behavior for Invoke parameters and return parameter
ParameterBuilder parameterBuilder = methodBuilder.DefineParameter(0, ParameterAttributes.Retval, "_ret");
if (marshalReturnAs is not null)
{
// set the return value marshal behavior
parameterBuilder.SetCustomAttribute(marshalReturnAs.ToCustomAttributeBuilder());
}
for (int i = 0; i < parameters.Length; ++i)
{
parameterBuilder = methodBuilder.DefineParameter(i + 1, ParameterAttributes.None, $"_{i}");
if (marshalParamAs[i] is not null)
{
// set the parameter marshal behavior
parameterBuilder.SetCustomAttribute(marshalParamAs[i]!.ToCustomAttributeBuilder());
}
}
// create the type and cache it so matching signatures (with matching marshaling) won't unnecessarily create new
// delegate types
Type type = typeBuilder.CreateType();
delegates[info] = type;
return type;
}
/// <summary>
/// Gets an instance of the runtime delegate type described by the given info.
/// </summary>
/// <param name="info">The native generic delegate info describing the runtime delegate.</param>
/// <param name="target">The new delegate target.</param>
/// <param name="method">The new delegate method.</param>
/// <returns>The delegate instance.</returns>
internal static Delegate GetDelegate(NativeGenericDelegateInfo info, object? target, MethodInfo method)
{
return Delegate.CreateDelegate(GetDelegateType(info), target, method);
}
/// <summary>
/// Gets an instance of the runtime delegate type described by the given info.
/// </summary>
/// <param name="info">The native generic delegate info describing the runtime delegate.</param>
/// <param name="functionPtr">The unmanaged function pointer to wrap with the new delegate.</param>
/// <returns>The delegate instance.</returns>
internal static Delegate GetDelegate(NativeGenericDelegateInfo info, nint functionPtr)
{
return Marshal.GetDelegateForFunctionPointer(functionPtr, GetDelegateType(info));
}
/// <summary>
/// Gets the runtime delegate <see cref="Type">Type</see> described by the given info.
/// </summary>
/// <param name="info">The native generic delegate info describing the runtime delegate.</param>
/// <returns>The runtime delegate type.</returns>
private static Type GetDelegateType(NativeGenericDelegateInfo info)
{
if (delegates.TryGetValue(info, out Type? type))
{
return type;
}
return DefineDelegateType(info);
}
/// <summary>
/// Gets the <see cref="INativeAction">INativeAction</see> or <see cref="INativeFunc{TResult}">INativeFunc</see>
/// interface type with the given return type and parameter types.
/// </summary>
/// <returns>The interface type.</returns>
private static Type GetInterfaceType(Type returnType, Type[] parameters)
{
bool isAction = returnType == typeof(void);
int parameterCount = parameters.Length;
if (parameterCount == 0)
{
return isAction ? typeof(INativeAction) : typeof(INativeFunc<>).MakeGenericType(returnType);
}
if (!isAction)
{
parameters = parameters.Append(returnType).ToArray();
}
return (parameterCount switch
{
/////////////////////////////////// * \\\\\\\\\\|////////// * \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
1 => isAction ? typeof(INativeAction<>) : typeof(INativeFunc<,>), /////////////////////////////////
2 => isAction ? typeof(INativeAction<,>) : typeof(INativeFunc<,,>), //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
3 => isAction ? typeof(INativeAction<,,>) : typeof(INativeFunc<,,,>), /////////////////////////////
4 => isAction ? typeof(INativeAction<,,,>) : typeof(INativeFunc<,,,,>), //\\\\\\\\\\\\\\\\\\\\\\\\\
5 => isAction ? typeof(INativeAction<,,,,>) : typeof(INativeFunc<,,,,,>), /////////////////////////
6 => isAction ? typeof(INativeAction<,,,,,>) : typeof(INativeFunc<,,,,,,>), //\\\\\\\\\\\\\\\\\\\\\
7 => isAction ? typeof(INativeAction<,,,,,,>) : typeof(INativeFunc<,,,,,,,>), /////////////////////
8 => isAction ? typeof(INativeAction<,,,,,,,>) : typeof(INativeFunc<,,,,,,,,>), //\\\\\\\\\\\\\\\\\
9 => isAction ? typeof(INativeAction<,,,,,,,,>) : typeof(INativeFunc<,,,,,,,,,>), /////////////////
10 => isAction ? typeof(INativeAction<,,,,,,,,,>) : typeof(INativeFunc<,,,,,,,,,,>), //\\\\\\\\\\\\
11 => isAction ? typeof(INativeAction<,,,,,,,,,,>) : typeof(INativeFunc<,,,,,,,,,,,>), ////////////
12 => isAction ? typeof(INativeAction<,,,,,,,,,,,>) : typeof(INativeFunc<,,,,,,,,,,,,>), //\\\\\\\\
13 => isAction ? typeof(INativeAction<,,,,,,,,,,,,>) : typeof(INativeFunc<,,,,,,,,,,,,,>), ////////
14 => isAction ? typeof(INativeAction<,,,,,,,,,,,,,>) : typeof(INativeFunc<,,,,,,,,,,,,,,>), //\\\\
15 => isAction ? typeof(INativeAction<,,,,,,,,,,,,,,>) : typeof(INativeFunc<,,,,,,,,,,,,,,,>), ////
16 => isAction ? typeof(INativeAction<,,,,,,,,,,,,,,,>) : typeof(INativeFunc<,,,,,,,,,,,,,,,,>), //
///////////////////////////////////// \|_\| ///////////\\\\\\\\\\\ \\__\\ ////
_ => throw new NotSupportedException("The maximum number of supported delegate parameters is 16.")
}).MakeGenericType(parameters);
}
/// <summary>
/// Converts a <see cref="MarshalAsAttribute">MarshalAsAttribute</see> to a <see
/// cref="CustomAttributeBuilder">CustomAttributeBuilder</see>.
/// </summary>
/// <param name="_this">The attribute to convert.</param>
/// <returns>The custom attribute builder.</returns>
/// <exception cref="UnreachableException">
/// An unexpected attribute field was encountered.
/// </exception>
private static CustomAttributeBuilder ToCustomAttributeBuilder(this MarshalAsAttribute _this)
{
FieldInfo[] fieldInfos = typeof(MarshalAsAttribute).GetFields(BindingFlags.Public | BindingFlags.Instance).Where
(f =>
{
return f.Name switch
{
nameof(MarshalAsAttribute.MarshalCookie) => _this.MarshalCookie is not null,
nameof(MarshalAsAttribute.MarshalType) => _this.MarshalType is not null,
nameof(MarshalAsAttribute.MarshalTypeRef) => _this.MarshalTypeRef is not null,
nameof(MarshalAsAttribute.SafeArrayUserDefinedSubType) => _this.SafeArrayUserDefinedSubType is not null,
_ => true
};
}).ToArray();
object?[] fieldValues = fieldInfos.Select
(f =>
{
return f.Name switch
{
nameof(MarshalAsAttribute.ArraySubType) => (object)(int)_this.ArraySubType,
nameof(MarshalAsAttribute.IidParameterIndex) => _this.IidParameterIndex,
nameof(MarshalAsAttribute.MarshalCookie) => _this.MarshalCookie,
nameof(MarshalAsAttribute.MarshalType) => _this.MarshalType,
nameof(MarshalAsAttribute.MarshalTypeRef) => _this.MarshalTypeRef,
nameof(MarshalAsAttribute.SafeArraySubType) => (int)_this.SafeArraySubType,
nameof(MarshalAsAttribute.SafeArrayUserDefinedSubType) => _this.SafeArrayUserDefinedSubType,
nameof(MarshalAsAttribute.SizeConst) => _this.SizeConst,
nameof(MarshalAsAttribute.SizeParamIndex) => _this.SizeParamIndex,
_ => throw new UnreachableException()
};
}).ToArray();
return new CustomAttributeBuilder
(
typeof(MarshalAsAttribute).GetConstructor(new[] { typeof(UnmanagedType) })!,
new object[] { _this.Value },
fieldInfos,
fieldValues
);
}
}
}