Skip to content

[API Proposal]: Reflection invoke support for FunctionPointer #75347

Open
@steveharter

Description

@steveharter

[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() if Type is a function pointer
  • FieldInfo.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 });

Metadata

Metadata

Assignees

Labels

Cost:MWork that requires one engineer up to 2 weeksapi-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Reflection

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions