-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
[primarily a placeholder for now for planning]
This adds invoke capability to function pointers, layering on See also #69273.
This will likely create a new MethodSignature class that will have an Invoke() method, analogous to Delegate.DynamicInvoke(https://docs.microsoft.com/dotnet/api/system.delegate.dynamicinvoke) and MethodBase.Invoke(https://docs.microsoft.com/dotnet/api/system.reflection.methodbase.invoke).
This enable dynamic invoke while supporting the CallConv* types for both existing function pointers and ones created dynamically.
Existing function pointers are be obtained from
typeof(SomeFunctionPointer)Type.GetType()ifTypeis a function pointerFieldInfo.GetMethodSignature()- The GetMethodSignature() methods are new and only work if the field\property\parameter is a function pointer.
PropertyInfo.GetMethodSignature()ParameterInfo.GetMethodSignature()
A MethodSignature can be created dynamically:
MethodSignature sig = new(
returnParameter: new MethodSignatureParameter(typeof(bool)),
parameters: new MethodSignatureParameter[] { new MethodSignatureParameter(typeof(int)) },
callingConventions = new Type[] {typeof(CallConv.Cdecl)});The Invoke() takes the function pointer as an IntPtr or void* along with the parameter values:
note that the sample below uses object-based parameters, but is expected to use strongly-typed
TypedReference and collections once that is available to avoid unnecessary allocations and to support by-ref-like types`:
delegate*<int, bool> fn = &MyFunctionPointer;
object retValue = sig.Invoke(fn, new object[] { 42 });