Skip to content

[API Proposal]: IL Emit support for FunctionPointer #75348

Closed

Description

@steveharter

Background and motivation

.NET has supported reflection introspection of function pointer types since .NET 8. This feature works well for existing type definitions, but currently there is no built-in way to construct System.Type instances representing arbitrary unmanaged function pointer types. This has implications in runtime code generation (System.Reflection.Emit), where it is currently difficult and unergonomic to define fields or locals of function pointer types.

Similarly, the EmitCalli method on ILGenerator is inadequately equipped for modern combinations of calling conventions on function pointers, due to the lack of flexibility of the CallingConvention enumeration. The proposed new overload modernizes this API and ensures it remains future-proof as new calling conventions get added to the runtime.

API Proposal

 namespace System;

 public abstract class Type
 {
     // Existing API
     public static Type MakeGenericSignatureType(Type genericTypeDefinition, params System.Type[] typeArguments);
     // Create function pointer signature type. It has functionality limited to what's required to represent signatures like other signature types.
+    public static Type MakeFunctionPointerSignatureType(Type? returnType, Type[]? parameterTypes, bool isUnmanaged = false, Type[]? callingConventions = null);

     // Create modified signature type. It has functionality limited to what's required to represent signatures like other signature types.
+    public static Type MakeModifiedSignatureType(Type type, Type[]? requiredCustomModifiers, Type[]? optionalCustomModifiers);

     // Existing API
     public virtual Type MakeGenericType(params Type[] typeArguments) => throw new NotSupportedException(SR.NotSupported_SubclassOverride);
     // Create full-featured function pointer type from the given type universe (`this` is the function pointer return type)
+    public virtual Type MakeFunctionPointerType(Type[]? parameterTypes, bool isUnmanaged = false) => throw new NotSupportedException(SR.NotSupported_SubclassOverride);
 }

 namespace System.Reflection.Emit;

 public abstract class ILGenerator
 {
     // Existing APIs
     public abstract void Emit(OpCode opcode, SignatureHelper signature);
     public abstract void EmitCalli(OpCode opcode, CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, Type[]? optionalParameterTypes);
     public abstract void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type? returnType, Type[]? parameterTypes);

     // Emit calli opcode with the signature specified by the function pointer type
     // Q: Should the signature have OpCode opcode argument for consistency with the existing APIs even though the only valid opcode is OpCode.Calli
+    public virtual void EmitCalli(System.Type functionPointerType) => throw new NotImplementedException();
 }

API usage

Type fieldType = Type.MakeFunctionPointerSignatureType(
    returnType: typeof(bool),
    parameterTypes: [typeof(int), typeof(string)],
    isUnmanaged: true,
    callingConventions: [typeof(CallConvCDecl), typeof(CallConvSuppressGCTransition)]);

TypeBuilder tb = ...;
tb.DefineField("MyField", fieldType, ...);

// ...

ILGenerator il = ...;
il.EmitCalli(fieldType);

Notes

  • The new overload of EmitCalli will not be supported by the legacy AssemblyBuilder implementation. It will only be implemented on DynamicILGenerator and ILGeneratorImpl.

Activity

  1. added
    api-needs-work
    on Sep 9, 2022
  2. added this to the 8.0.0 milestone on Sep 9, 2022
  3. self-assigned this
    on Sep 9, 2022
  4. added
    Cost:M
    on Dec 5, 2022
  5. modified the milestones: 8.0.0, 9.0.0 on Aug 2, 2023
  6. modified the milestones: 9.0.0, 10.0.0 on Jul 24, 2024
  7. removed this from the 10.0.0 milestone on Jul 26, 2025
  8. 41 remaining items

  9. jkotas commented on Jan 31, 2026

    @jkotas
    Member

    For example, you can create an array of function pointers that take a return a type that is only known at runtime:

    Array CreateArrayOfFunctionPointers(Type t, int length)
    {
        return Array.CreateInstance(t.MakeFunctionPointerType(new Type[] { t }), length);
    }
    

    I do not think you can do that with MakeFunctionPointerSignatureType.

  10. AaronRobinsonMSFT commented on Feb 1, 2026

    @AaronRobinsonMSFT
    Member

    I do not think you can do that with MakeFunctionPointerSignatureType.

    Is this because the returned Type is merely for signature expression rather than use as a Type? Whereas with MakeFunctionPointerType(), the result is a real Type (that is, RuntimeType)?

  11. jkotas commented on Feb 1, 2026

    @jkotas
    Member

    Yes, Array.CreateInstance is going to throw if you pass in a type that is not RuntimeType.

  12. jgh07 commented on Feb 1, 2026

    @jgh07
    Contributor

    For example, you can create an array of function pointers that take a return a type that is only known at runtime

    Thanks for the example, I hadn't considered that.

  13. hamarb123 commented on Feb 10, 2026

    @hamarb123
    Contributor

    Is it possible to create static vs hasthis vs hasthis+explicitthis managed function pointer types using this API?

  14. jkotas commented on Feb 10, 2026

    @jkotas
    Member

    static vs hasthis vs hasthis+explicitthis managed function pointer types using this API

    It is not possible. Reflection does not have representations for these flags in function pointers. It would be a different proposal.

  15. added a commit that references this issue on Mar 4, 2026
  16. modified the milestones: 11.0.0, 12.0.0, Future on Jul 28, 2026
  17. jgh07 commented on Aug 24, 2026

    @jgh07
    Contributor

    Quick question regarding Mono. I opened a tracking issue in #124149 and looked into it at the time, but as far as I remember, adding these APIs would have required extensive additional runtime work.

    However, the issue has since been closed without comment, which leads me to assume that a Mono implementation is not currently planned. @jkotas and others, would it be possible to get a public status update on this? I think it would be beneficial to have official reasoning for why the issue was closed.

    I assume that if Mono support is not planned, this issue can be closed.

  18. AaronRobinsonMSFT commented on Aug 24, 2026

    @AaronRobinsonMSFT
    Member

    I assume that if Mono support is not planned, this issue can be closed.

    @jgh07 Thanks for keeping us honest about messaging. Your assumption is correct, there are no plans to add this support to Mono.

  19. locked and limited conversation to collaborators on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Cost:Mapi-approvedarea-System.Reflection.Emit

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions