[API Proposal]: Reflection invoke support for FunctionPointer
Opened this issue · 1 comments
[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()
ifType
is 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 });
Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.
Issue Details
[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()
ifType
is 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 });
Author: | steveharter |
---|---|
Assignees: | steveharter |
Labels: |
|
Milestone: | 8.0.0 |