Редактиране

Споделяне чрез


IntPtr no longer used for function pointer types

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

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

This new functionality is currently implemented in the CoreCLR runtime and in MetadataLoadContext. Support for the Mono and NativeAOT runtimes is 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 has changed.

Previous behavior

Previously, typeof(delegate*<void>()) returned the System.IntPtr type for a function pointer type. Similarly, reflection also returned this type for a function pointer type, such as with FieldInfo.FieldType. The IntPtr type didn't allow any access to the parameter types, return type, or calling conventions.

New behavior

typeof and reflection now use System.Type for a function pointer type, which provides access to the parameter types, return type, and calling conventions.

Version introduced

.NET 8 Preview 2

Type of breaking change

This change is a behavioral change.

Reason for change

This 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 5, but reflection support wasn't added at that time.

If you want your code to support function pointers and to treat them specially, use the new Type.IsFunctionPointer API.

Affected APIs