[Breaking change]: Unintentional UnsafeAccessor
support for non-open generics is altered in .NET 9.
#41769
Closed
1 of 3 tasks
Labels
breaking-change
Indicates a .NET Core breaking change
doc-idea
Indicates issues that are suggestions for new topics [org][type][category]
Pri1
High priority, do before Pri2 and Pri3
📌 seQUESTered
Identifies that an issue has been imported into Quest.
Description
In .NET 8, we introduced the
UnsafeAccessor
API. This permitted access to non-visible members of types (a.k.a., "fast private reflection") - see [API Proposal]: Zero-overhead member access with suppressed visibility checks · Issue #81741 · dotnet/runtime (github.com). The initial design supported generics, but we didn't add support in .NET 8 due to time constraints. However, in CoreCLR and native AOT, some very narrow and not officially supported scenarios did work. These scenarios weren't intentionally blocked, but should have been.In .NET 9, official support was added with dotnet/runtime#99468.
Version
.NET 9 Preview 6
Previous behavior
A naive signature look-up on types was implemented, which caused use of generic types to be valid in some cases. For example, the below should have been deemed invalid:
New behavior
The fully supported and documented way to consume generic types is to ensure type parameters match type parameters and method parameters match method parameters. This is because the runtime performs a strict metadata signature match. Examples and remarks can be found here.
Type of breaking change
Reason for change
The reasoning for the change is that the previous behavior was not intended to be supported in the official release. It was, early in development, a possibly supported scenario, but was later deferred until .NET 9 for complexity issues encountered. The official documentation did not mention generics nor provide any examples using generics.
Recommended action
Users are encouraged to examine the updated documentation for the
UnsafeAccessorAttribute
API. Support for this scenario in .NET 8 is considered unintentional and uses deemed an error.Feature area
Core .NET libraries
Affected APIs
System.Runtime.CompilerServices.UnsafeAccessorAttribute
Associated WorkItem - 286111
The text was updated successfully, but these errors were encountered: