Skip to content

[Breaking change]: System.Reflection no longer uses the IntPtr type for function pointer types #34623

@steveharter

Description

@steveharter

Description

As a new reflection feature, a function pointer type will be a System.Type instance with new capabilities such as Type.IsFunctionPointer. Previously, the System.Type instance returned was the IntPtr type.

Using System.Type is this manner is similar to how other types are exposed such as pointers (Type.IsPointer) and arrays (Type.IsArray).

For more information, see the API issue and Design.

This new functionality is currently implemented in the CoreCLR runtime and in System.Reflection.MetadataLoadContext. Support for the Mono and NativeAOT runtimes are expected later.

A function pointer instance, which is a physical address to a function, continues to be represented as an IntPtr; only the reflection type was changed with this feature.

Version

.NET 8 Preview 2

Previous behavior

The IntPtr type was used for a function pointer type such as with typeof(delegate*<void>()) or when obtaining a function pointer type through reflection such as with FieldInfo.FieldType. This did not allow any access to the parameter types, return type or calling conventions.

New behavior

The System.Type is now used for a function pointer type with access to the parameter types, return type and calling conventions.

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
  • Behavioral change: Existing binaries may behave differently at run time.

Reason for change

Adds the capability to obtain function pointer metadata including parameter types, the return type and the calling conventions. Function pointer support was added with C# 9 and .NET 6 but reflection support was not added at that time.

Recommended action

Any general-purpose reflection code that wants to support function pointers should use the new API Type.IsFunctionPointer if special treatment is required for function pointers.

Feature area

Other (please put exact area in description textbox)

Affected APIs

For function pointer types, the C# keyword typeof and these reflection APIs now return a Type instance with new capabilities\APIs instead of the semantics returned by the IntPtr type.

namespace System.Reflection
{
    public abstract class FieldInfo
    {
        public Type FieldType() {get;}
    }
    public abstract class PropertyInfo
    {
        public Type PropertyType() {get;}
    }
    public abstract class ParameterInfo
    {
        public Type ParameterType() {get;}
    }
}

Associated WorkItem - 70834

Metadata

Metadata

Assignees

Labels

📌 seQUESTeredIdentifies that an issue has been imported into Quest.breaking-changeIndicates a .NET Core breaking change

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions