4주 전 · Jul 26, 2026 1:54 PM
Accessing private class members in .NET 8 without System.Reflection latency:
CSHARP
public class CoreService
{
private int _connectionCount = 42;
private void ResetInternalState() { /* reset */ }
}
// UnsafeAccessor generates direct IL access with 0 reflection overhead!
public static class Accessors
{
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_connectionCount")]
public extern static ref int GetConnectionCount(CoreService service);
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "ResetInternalState")]
public extern static void ResetState(CoreService service);
}Executes as fast as direct public member access and is 100% compatible with NativeAOT!