How C# Decompilers Recover File-Local Types
Resumo
Pixel shares a DEV Community article on how C# 11 compiles file-local types into mangled metadata names with assembly-internal visibility. It explains how decompilers can recognize names such as <Validators>F0__Parser, restore the original type name, and represent its file-scoped visibility—important for accurately understanding source-generated code.
DEV Community
Decompiling file-local types: recovering the file modifier from a mangled metadata name
C# 11 added the file access modifier: a type declared file class Parser is visible only inside the single source file that declares it. Two files in the same project — or two source-generated files that each emit a helper — can both declare a file class Parser without colliding, and neither can be touched from anywhere else. It exists mostly for the benefit of source generators, which emit support types into a user's compilation and must not leak names that clash with the user's own code or with another generator's. From a decompiler's point of view file is an interesting problem because, like several recent C# features, it leaves no attribute behind. There is no [FileLocal] marker, no custom modifier, no flag in the TypeDef row that says "this type was declared file." The compiler encodes the modifier in the one place that survives into metadata and is guaranteed unique: the type's name. It mangles the declared name into a form that is deliberately not a legal C# identifier, and it drops the visibility down to assembly-internal. A decompiler that reads the TypeDef table and prints what it finds will emit a type called F0__Parser with internal accessibility — which is neither what the author wrote nor anything the C# compiler will accept. Reconstructing the original means recognising the mangling, reversing it, and putting the recovered type back in a file bucket. This post walks the full round trip: the source, the metadata the compiler emits, and the C# Glass.NET reconstructs. What the author wrote A file type usually keeps a small helper private to one file. Here Validators.cs has a file-local rule plus a public façade that uses it: // Validators.cs
file sealed class PhoneRule
{
public static bool IsValid(string s) => s.Length == 10;
}
public static class Contact
{
public static bool Check(string phone) => PhoneRule.IsValid(phone);
}
PhoneRule is file-scoped, so nothing outside Validators.cs can name it — not another file in the same assembly, not a consumer referencing the DLL. Contact, declared normally, calls it freely because it lives in the same file. What the compiler emits Two things happen to PhoneRule on the way to metadata. First, its name is mangled. The compiler builds a metadata name from a file identifier (derived from the source file's name), an ordinal that disambiguates files that happen to share a name, and the declared type name, wrapped so the result cannot be written in source: .class private sealed auto ansi beforefieldinit
'F0__PhoneRule'
extends [System.Runtime]System.Object
{
.method public hidebysig static
bool IsValid (string s) cil managed
{
ldarg.0
callvirt instance int32 [System.Runtime]System.String::get_Length()
ldc.i4.s 10
ceq
ret
}
}
Note what is not there: no CompilerGeneratedAttribute, no custom attribute of any kind announcing the modifier. The F0__PhoneRule name is the entire record of the file keyword. The < and > are the tell — they are reserved exactly so that user code can never produce an identifier that collides with a compiler-synthesized one. Second, the type's accessibility drops to assembly-internal. A top-level type can only be public or internal in the CLI type system — there is no finer-grained visibility at the metadata level — so the compiler emits PhoneRule as internal and relies on the unspeakable name to enforce the stronger "one file only" rule at compile time. In ILDasm a top-level internal type reads as .class private, which is why the row above says private even though you never wrote private. The call site inside Contact.Check references the mangled name directly, because by the time IL is emitted the only name PhoneRule has is the mangled one: .method public hidebysig static bool Check (string phone) cil managed
{
ldarg.0
call bool 'F0__PhoneRule'::IsValid(string)
ret
}
Every reference to a file-local type inside its own file — base types, field...

Powered by Readybot.io