|
| 1 | +--- |
| 2 | +title: "Breaking change: IntPtr no longer used for function pointer types" |
| 3 | +description: Learn about a breaking change in .NET 8 SDK where System.Reflection uses a System.Type instance to represent a function pointer. |
| 4 | +ms.date: 03/17/2023 |
| 5 | +--- |
| 6 | +# IntPtr no longer used for function pointer types |
| 7 | + |
| 8 | +As a new reflection feature, a function pointer type is now a <xref:System.Type?displayProperty=nameWithType> instance with new capabilities such as <xref:System.Type.IsFunctionPointer?displayProperty=nameWithType>. Previously, the <xref:System.Type?displayProperty=nameWithType> instance returned was the <xref:System.IntPtr> type. |
| 9 | + |
| 10 | +Using <xref:System.Type?displayProperty=nameWithType> in this manner is similar to how other types are exposed, such as pointers (<xref:System.Type.IsPointer?displayProperty=nameWithType>) and arrays (<xref:System.Type.IsArray?displayProperty=nameWithType>). |
| 11 | + |
| 12 | +This new functionality is currently implemented in the CoreCLR runtime and in <xref:System.Reflection.MetadataLoadContext>. Support for the Mono and NativeAOT runtimes is expected later. |
| 13 | + |
| 14 | +A function pointer instance, which is a physical address to a function, continues to be represented as an <xref:System.IntPtr>; only the reflection type has changed. |
| 15 | + |
| 16 | +## Previous behavior |
| 17 | + |
| 18 | +Previously, `typeof(delegate*<void>())` returned the <xref:System.IntPtr?displayProperty=fullName> type for a function pointer type. Similarly, reflection also returned this type for a function pointer type, such as with <xref:System.Reflection.FieldInfo.FieldType?displayProperty=nameWithType>. The <xref:System.IntPtr> type didn't allow any access to the parameter types, return type, or calling conventions. |
| 19 | + |
| 20 | +## New behavior |
| 21 | + |
| 22 | +`typeof` and reflection now use <xref:System.Type?displayProperty=fullName> for a function pointer type, which provides access to the parameter types, return type, and calling conventions. |
| 23 | + |
| 24 | +## Version introduced |
| 25 | + |
| 26 | +.NET 8 Preview 2 |
| 27 | + |
| 28 | +## Type of breaking change |
| 29 | + |
| 30 | +This change is a [behavioral change](../../categories.md#behavioral-change). |
| 31 | + |
| 32 | +## Reason for change |
| 33 | + |
| 34 | +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 6, but reflection support wasn't added at that time. |
| 35 | + |
| 36 | +## Recommended action |
| 37 | + |
| 38 | +If you want your code to support function pointers and to treat them specially, use the new <xref:System.Type.IsFunctionPointer?displayProperty=nameWithType> API. |
| 39 | + |
| 40 | +## Affected APIs |
| 41 | + |
| 42 | +- `typeof` keyword |
| 43 | +- <xref:System.Reflection.FieldInfo.FieldType?displayProperty=fullName> |
| 44 | +- <xref:System.Reflection.PropertyInfo.PropertyType?displayProperty=fullName> |
| 45 | +- <xref:System.Reflection.ParameterInfo.ParameterType?displayProperty=fullName> |
0 commit comments