Repository navigation
Description
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
EmitCalliwill not be supported by the legacyAssemblyBuilderimplementation. It will only be implemented onDynamicILGeneratorandILGeneratorImpl.
Activity
41 remaining items
jkotas commentedon Jan 31, 2026
For example, you can create an array of function pointers that take a return a type that is only known at runtime:
I do not think you can do that with
MakeFunctionPointerSignatureType.AaronRobinsonMSFT commentedon Feb 1, 2026
Is this because the returned
Typeis merely for signature expression rather than use as aType? Whereas withMakeFunctionPointerType(), the result is a realType(that is,RuntimeType)?jkotas commentedon Feb 1, 2026
Yes,
Array.CreateInstanceis going to throw if you pass in a type that is notRuntimeType.jgh07 commentedon Feb 1, 2026
Thanks for the example, I hadn't considered that.
hamarb123 commentedon Feb 10, 2026
Is it possible to create static vs hasthis vs hasthis+explicitthis managed function pointer types using this API?
jkotas commentedon Feb 10, 2026
It is not possible. Reflection does not have representations for these flags in function pointers. It would be a different proposal.
New function pointer APIs (#123819)
jgh07 commentedon Aug 24, 2026
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.
AaronRobinsonMSFT commentedon Aug 24, 2026
@jgh07 Thanks for keeping us honest about messaging. Your assumption is correct, there are no plans to add this support to Mono.