Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tracked by #75358
steveharter opened this issue Sep 9, 2022 · 1 comment
Open
Tracked by #75358

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

steveharter opened this issue Sep 9, 2022 · 1 comment
Assignees
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Reflection Cost:M Work that requires one engineer up to 2 weeks
Milestone

Comments

@steveharter
Copy link
Member

[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 });
@steveharter steveharter added api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Reflection labels Sep 9, 2022
@steveharter steveharter added this to the 8.0.0 milestone Sep 9, 2022
@steveharter steveharter self-assigned this Sep 9, 2022
@ghost
Copy link

ghost commented Sep 9, 2022

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() 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 });
Author: steveharter
Assignees: steveharter
Labels:

api-needs-work, area-System.Reflection

Milestone: 8.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Reflection Cost:M Work that requires one engineer up to 2 weeks
Projects
None yet
Development

No branches or pull requests

1 participant