Why typeof survives obfuscation but strings don't
typeof(T) compiles to ldtoken plus a call to GetTypeFromHandle — a metadata token that points straight at the type's row in the TypeDef table, so renaming the row's Name never breaks it. Type.GetType("MyApp.Orders.Invoice") passes a literal from the #US user-string heap that has no link to any row, so after renaming it matches nothing and returns null. This is why typeof keeps resolving, why nameof still breaks, and how to fix reflection-by-string.
You renamed every type and method in your assembly, rebuilt, and ran the test suite. Most of it passed. But one feature — the one that loads handlers reflectively — throws NullReferenceException on a call that worked yesterday, while a different piece of reflection two files over keeps resolving types perfectly. Same obfuscator, same renaming pass, opposite outcomes. The difference is not luck. It is whether the code asked for a type by token or by string, and that difference is visible in the IL.
The real question
Both of these look like “get me a type at run time”:
Type a = typeof(Invoice); // survives renaming
Type? b = Type.GetType("MyApp.Orders.Invoice"); // returns null after renaming
After Nebula.NET renames Invoice to something like b, the first line still hands you the right Type and the second returns null. To see why, you have to look at what the C# compiler actually emits for each — because they do not compile to anything alike.
The mechanism, shown
typeof(T) does not carry the name of the type. It compiles to a single ldtoken instruction whose operand is a metadata token: a 4-byte handle whose high byte names a table (0x02 = TypeDef) and whose low three bytes are a row index into that table. ldtoken pushes a RuntimeTypeHandle for that row; a call to System.Type::GetTypeFromHandle turns the handle into a Type.
// typeof(Invoice)
ldtoken MyApp.Orders.Invoice // metadata token -> TypeDef row, e.g. 0x02000007
call class [System.Runtime]System.Type::GetTypeFromHandle(valuetype System.RuntimeTypeHandle)
The operand is the row, not the text. The row has a Name column (a string-heap offset) that currently reads Invoice, but the ldtoken does not point at that string — it points at the row. When the renamer rewrites the row’s Name to b, the row keeps its identity and its token. typeof(Invoice) still loads token 0x02000007, still resolves the same row, and now hands you a Type whose Name is b. Nothing had to be rewritten for it to keep working; renaming updates every token-bearing reference as a matter of course.
Type.GetType("MyApp.Orders.Invoice") is a different machine entirely. The argument is a plain string literal, emitted as ldstr into the #US (user-string) heap, with no metadata link to any TypeDef row:
// Type.GetType("MyApp.Orders.Invoice")
ldstr "MyApp.Orders.Invoice" // #US heap literal, not a token
call class [System.Runtime]System.Type::GetType(string)
At run time GetType parses that string and walks the metadata looking for a type whose namespace + Name match it. Before renaming, a row named Invoice matches and you get the type. After renaming, that row’s Name is b — so the literal "MyApp.Orders.Invoice" matches nothing, and GetType returns null. The renamer never touched the literal (a string is not a reference, so it is left alone by design), and even if it had, the string has no link telling it which row to follow. The token knows its row; the string only knows a name that no longer exists.
The same split for members
Members behave identically, and one line shows both halves at once:
MethodInfo? m = typeof(Invoice).GetMethod(nameof(Invoice.Total));
The typeof(Invoice) is a token and survives — it resolves the renamed type correctly. But GetMethod takes a string, and nameof(Invoice.Total) is a trap dressed as safety: the C# compiler resolves it at compile time to the literal "Total" and emits an ldstr, exactly like a hand-typed "Total" would. There is no token in a nameof.
ldtoken MyApp.Orders.Invoice // typeof -> token, survives
call class [System.Runtime]System.Type::GetTypeFromHandle(valuetype System.RuntimeTypeHandle)
ldstr "Total" // nameof(Invoice.Total) -> literal, NOT a token
callvirt instance class [System.Reflection]System.Reflection.MethodInfo [System.Runtime]System.Type::GetMethod(string)
Rename Total to a and the type still resolves while GetMethod("Total") returns null. That is the rule in one line: token-carrying constructs survive renaming; anything that re-finds a member by its original string name breaks. typeof, sizeof, a method-group delegate (Func<int> f = invoice.Total;), and a direct call all carry tokens and ride through renaming untouched. GetType(string), GetMethod(string), GetProperty(string), and every nameof fed into them are strings and break.
How it applies
Once you can read the split in the IL, fixing reflection-by-string is mechanical.
Refactor to tokens where you can. The most robust fix is to stop re-finding the member by name. Replace Type.GetType("MyApp.Orders.Invoice") with typeof(Invoice). Replace GetMethod("Total") + Invoke with a method-group delegate or a direct call. Token-based access needs no obfuscation configuration at all, because renaming updates the tokens for you.
Keep-list what must stay a string. Some names are strings because something outside the assembly names them too — a plugin loaded by its configured type name, a contract another component resolves. For those, exclude exactly the reflected members from renaming so their Name stays stable and the literal keeps matching:
{
"inputs": ["bin/Release/net8.0/App.dll"],
"outputDirectory": "bin/Release/net8.0/obf",
"renameIdentifiers": true,
"keep": ["MyApp.Orders.Invoice", "MyApp.Orders.Invoice.Total"]
}
The craft is in keeping that list minimal — every entry is a name you are choosing not to protect. Nebula.NET’s Pro tier gives you the finer-grained exclusions (by member, by attribute, by pattern) so the keep-list stays as small as the reflection genuinely requires. The mechanics of writing good exclusions are their own topic — see keep-lists and rename exclusions.
Caveats — where it bites
nameoflooks safe and is not. It is a compile-time string literal, so it both leaks the original name in plaintext and breaks when the member is renamed. There is no token in it. The leak side of this — your original names sitting in the#USheap — is covered in the features that leak renamed members.- Attributes that store type names as strings break. A serializer writing a
$typediscriminator ("MyApp.Orders.Invoice, MyApp") or a container wired by string type name is resolving byName, not by token, so renaming breaks deserialization and resolution the same wayGetTypebreaks. These serialization pitfalls have their own write-up: reflection and serialization under obfuscation. - Generic instantiations via tokens survive.
typeof(Repository<Invoice>)emits anldtokenfor the constructed type, and the type arguments are themselves token references, so renaming updates every part. Generics are not a special case — they follow the token rule.
Takeaway
The question “will my reflection survive obfuscation?” has one deterministic answer, and it is in the IL. If the construct compiles to ldtoken — typeof, sizeof, a method group, a direct call — it carries a metadata token that points at the member’s row, and renaming follows that row wherever its name goes. If it compiles to ldstr and then re-finds the member by that string — GetType, GetMethod, nameof fed to either — it carries a name, not a row, and renaming leaves the name behind. Refactor to tokens where you can, keep-list the few names that must stay strings, and you get aggressive renaming that protects your IP without quietly nulling out the reflection you forgot about.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.