dotnet / dotnet/runtime

`UnsafeAccessorTypeAttribute` support for field accessors

Open
#119,664 3 comments 7 reactions 0 assignees View on GitHub
area-System.Runtime.CompilerServices
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

The initial proposal in #90081 mentioned that the inaccessible type problem applies to both fields and methods, but the implementation skipped support for fields due to type safety issues: https://github.com/dotnet/runtime/pull/114881#issuecomment-2856649342 - having a `ref object` return type for the accessor isn't memory safe, it would need `TypedReference` support for that.

Needing it for fields is probably less common, but I just ran into a situation where I need it for .NET 10 (as a replacement for `HttpContent.TryGetBuffer` that's in older versions):
```cs
[UnsafeAccessor(UnsafeAccessorKind.Field)]
[return: UnsafeAccessorType("System.Net.Http.HttpContent+LimitArrayPoolWriteStream, System.Net.Http")]
private static extern ref object? _bufferedContent(HttpContent content);
```
This ends up throwing `Invalid usage of UnsafeAccessorTypeAttribute`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.