Decompiling ref fields and scoped: how C# 11 ref structs look in IL
DEV Community
Decompiling ref fields and scoped: how C# 11 ref structs look in IL
A ref field is the first time a C# field stores a reference rather than a value, and it compiles to metadata that earlier decompilers never had to read. The field''s signature carries a byref element type — ELEMENT_TYPE_BYREF inside a FIELD signature, which was illegal before .NET 7 — and the ref-safety rules that keep it from outliving what it points at are encoded in modreq and attributes, not in IL you can execute. scoped is the loudest of those: it is a ScopedRefAttribute on a parameter, with no runtime effect at all, that a decompiler either surfaces as the scoped keyword or drops, quietly changing the method''s ref-safety contract. This post reads the raw signatures and attribute blobs for a ref field, a ref struct that holds one, and a scoped ref parameter, shows where the ref escape rules live in metadata, and walks through how Glass.NET reconstructs ref, ref readonly, scoped and the [UnscopedRef] escape hatch so the decompiled source compiles with the same safety the author wrote.
0 comments
No comments yet.