How Glass rebuilds nested generic type names
The name a generic type carries in metadata is mangled with a backtick and a number, and that number counts only the parameters the type adds for itself — a nested type inherits its enclosing type's parameters but does not count them in its name. This post reads the real encoding, shows why the flat generic-argument vector has to be split across nesting levels, and explains how Glass.NET turns Outer`2/Inner`1 back into Outer<int, string>.Inner<bool>.
Decompile a type nested inside a generic type and the name you get back — Outer<int, string>.Inner<bool> — looks nothing like the name stored on disk. The metadata holds Inner1`, with a backtick and a number, and that number is not three, even though three type parameters are in scope inside the type. The gap between those two spellings is one of the small, precise things a decompiler has to get exactly right, because getting it wrong produces C# that does not compile.
The real question
A generic type name in .NET metadata is mangled: List1, Dictionary2, Func4`. The backtick and the number encode the type’s generic arity. That rule is simple until types nest. A type nested inside a generic type can use the enclosing type’s parameters as if they were its own — but its name does not count them. So where does the full picture live, and how does a reader get from a mangled name plus a flat list of type arguments back to the fully-qualified generic name a developer would recognise?
Arity counts what the type adds, not what it has
Take a generic type with a generic type nested inside it:
public class Outer<TKey, TValue>
{
public class Inner<TState>
{
public TValue Get(TKey key, TState state) => default;
}
}
Inner uses three type parameters — TKey and TValue from Outer, plus its own TState. But its metadata name does not reflect that. Here is how the two types are stored, rendered the way Glass shows them in the IL view:
.class public auto ansi beforefieldinit Outer`2<TKey, TValue>
extends [System.Runtime]System.Object
{
.class auto ansi nested public beforefieldinit Inner`1<TState>
extends [System.Runtime]System.Object
{
// inside Inner the parameter index space is [TKey=!0, TValue=!1, TState=!2]
.method public hidebysig instance !1 Get(!0 key, !2 state) cil managed
{
// returns TValue (!1), takes TKey (!0) and TState (!2)
}
}
}
Outer is Outer2— it adds two parameters.InnerisInner1 — it adds one. The 1 is TState alone. TKey and TValue are inherited, and the name does not count inherited parameters.
Yet look at the method signature. Get returns TValue and that is encoded as !1 — generic parameter index 1. Its arguments are !0 (TKey) and !2 (TState). The index space inside Inner runs 0, 1, 2 across all three parameters, enclosing first. So Inner has an arity of three for the purpose of signatures, but a name that says one. That is not a contradiction — it is two different numbers for two different jobs.
The two numbers, in the tables
The name lives in the #Strings heap as the literal text Inner1. It is just a string; it carries no information about what Inneris nested in. The nesting is a separate fact, stored in the **NestedClass** table — one row linkingInner's TypeDef to Outer`‘s TypeDef. Walk that table and you learn the enclosing chain; read the name and you learn the own-arity. Neither knows about the other.
The third number is in the GenericParam table. Every generic parameter in scope for a type gets a row owned by that type, numbered from zero. For Inner, the table holds three rows — TKey (0), TValue (1), TState (2) — the enclosing parameters re-listed first, then the type’s own. That is why !0 resolves to TKey inside Inner’s method bodies. So the same nested type carries an own-arity of one in its name and a full parameter count of three in GenericParam. A decompiler has to hold both in mind at once.
Splitting the flat argument vector
Now construct the type and the real work appears:
Outer<int, string>.Inner<bool> x = new();
The CLR encodes Outer<int, string>.Inner<bool> as a single GenericInst signature: the innermost type reference, a generic-argument count of 3, and a flat vector of three arguments — int, string, bool. There is no grouping in the bytes. Nothing says that int and string belong to Outer and bool belongs to Inner. The signature is flat precisely because the parameter index space is flat.
To print a legal C# name, Glass has to re-group that flat vector across the nesting levels, and the only thing that tells it where the cuts go is each level’s own-arity — the backtick number it stripped off the name. Outer2takes the first two arguments;Inner1 takes the next one. Consume them in enclosing-to-nested order and the vector is partitioned exactly.
The grouping has to be correct or the output is garbage. If Glass naively gave all three arguments to the innermost name it would render Inner<int, string, bool> — a type that does not exist. If it gave the first argument to Outer and the rest to Inner it would render Outer<int>.Inner<string> and quietly drop bool. The backtick arities are the partition keys, and they are the whole reason the name mangling carries the own-count rather than the total: the own-count is exactly what you need to walk the flat vector level by level.
The separators are all display
One more thing the bytes do not store: the separator between levels. Reflection’s Type.FullName joins nested types with + (App.Outer2+Inner1), ILAsm uses / (Outer2/Inner1), and C# source uses . (Outer.Inner). All three are rendered on top of the same NestedClass row. Glass emits . in its C# output and preserves + or / in the token and IL views, so the name you read always matches the surface you are reading it on.
Reading it from the CLI
You do not need the UI to see the split happen. Documentation-comment member IDs keep the mangled, backtick form, so that is how you address a nested generic type:
$ glass export ./App.dll --member "T:App.Outer`2.Inner`1" --il
The IL view shows Outer2and the nestedInner1 with the !0/!1/!2 parameter indices intact. Drop --il and the reconstructed C# prints Outer<TKey, TValue>.Inner<TState> for the open definition, with the backticks stripped and each level’s angle brackets filled from its own slice of the parameter list. Point Glass at a call site that constructs the closed type and the same machinery renders Outer<int, string>.Inner<bool>.
Caveats and where it gets hard
A couple of honest limits. The backtick arity is a C# convention, not a CLR rule — a language that is not C#, or hand-written IL, can emit a generic type whose name has no backtick at all, or a backtick count that disagrees with its GenericParam rows. When that happens Glass trusts the GenericParam table and the NestedClass chain, not the string, because the tables are what the runtime actually binds against. And deeply nested chains — a generic nested in a generic nested in a generic — just extend the same split: read each level’s own-arity, consume that many arguments, recurse. The arithmetic is simple; the discipline is never confusing own-arity with total arity, because the metadata hands you both and uses them for different things.
Takeaway
A nested generic type is stored as three separate facts that never touch each other: a mangled name in the #Strings heap that counts only the parameters the type adds, a NestedClass row that records what it is nested in, and a GenericParam list that holds every parameter in scope. Constructing it produces a fourth — a flat, ungrouped vector of type arguments. Glass.NET reads all four, walks the enclosing chain, splits the flat vector by each level’s own-arity, and joins the result with the right separator for the view you are in. It is a small corner of the metadata, and it is why decompiling a nested generic name well means reading the tables, not trusting the string. Grab the free build from the Glass.NET download page and open the IL view on the next Outer<...>.Inner<...> you need to read.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.