Why a stored type name breaks after renaming
A plugin app that resolves "Acme.Exporters.PdfExporter, Acme.Exporters" with Type.GetType and Activator.CreateInstance works in debug and throws in production the moment you obfuscate it. The reason is a mismatch between a metadata token and a plain string — and the fix is a rename-safe registry that keeps zero names.
Here is a plugin loader that has shipped in a thousand .NET apps. The app reads an exporter’s type name from configuration, resolves it, and creates an instance:
// appsettings: "Exporter": "Acme.Exporters.PdfExporter, Acme.Exporters"
string typeName = config["Exporter"];
Type? t = Type.GetType(typeName);
var exporter = (IExporter)Activator.CreateInstance(t!)!;
exporter.Export(report);
It passes every test. You obfuscate the build, ship it, and the first customer who selects the PDF exporter gets a NullReferenceException — because Type.GetType returned null. Nothing in the logic changed. The type is still in the assembly. So what moved?
The real question: token or string?
Renaming is the backbone of obfuscation: it rewrites PdfExporter to a, Acme.Exporters to nothing, across the whole assembly. The reason a normal method call survives this is that the compiler never stored the name of what it calls — it stored a metadata token, a numeric handle into the assembly’s type and member tables. When the obfuscator renames a type, it rewrites the token’s target row and every token that points at it in lockstep. The link is numeric, so it cannot drift.
A string is not a token. Type.GetType("Acme.Exporters.PdfExporter, Acme.Exporters") hands the loader a run of characters and asks it to search the metadata at runtime for a type whose namespace and name spell that out. The string lives in the assembly’s user-string heap as inert data. Renaming rewrites the type’s Name and Namespace columns in the TypeDef table — but it has no reason to touch a string literal, and no safe way to know that this particular literal was meant to name that particular type. So the string still says Acme.Exporters.PdfExporter, the TypeDef row now says a, the search matches nothing, and you get null.
That is the whole bug. It is the same fault line as reflection-by-member-name, but one level up: here the type identity itself is the thing carried as a string.
Seeing it in the IL
Compare how the two ways of naming a type compile. A typeof(PdfExporter) lowers to a token load:
ldtoken Acme.Exporters.PdfExporter
call class System.Type System.Type::GetTypeFromHandle(valuetype System.RuntimeTypeHandle)
The ldtoken operand is a token into the TypeDef/TypeRef table. Rename the type and the obfuscator rewrites both the row and this operand together — the handle still points at the right type.
Type.GetType(string) lowers to a string load:
ldstr "Acme.Exporters.PdfExporter, Acme.Exporters"
call class System.Type System.Type::GetType(string)
The ldstr operand points into the user-string heap. The obfuscator leaves it alone. (String encryption would replace this ldstr with a decrypt call, but the decrypted value is the same text — so it changes the static picture, not the lookup, and the resolution still fails.) Meanwhile the TypeDef row the string was meant to find has changed underneath it:
Note what did not break: Activator.CreateInstance(t). Once you hold a real Type, Activator calls its constructor by token, and constructors are never renamed. The entire failure is concentrated in the one string-to-Type step.
The fix: resolve by token, store a key
The robust solution is to stop carrying a CLR type name as data at all. Carry a stable key you choose — one that has nothing to do with what the type is called in the assembly — and map it to the type through typeof():
static readonly Dictionary<string, Type> Exporters = new()
{
["pdf"] = typeof(PdfExporter),
["csv"] = typeof(CsvExporter),
["xlsx"] = typeof(XlsxExporter),
};
// appsettings: "Exporter": "pdf"
Type t = Exporters[config["Exporter"]];
var exporter = (IExporter)Activator.CreateInstance(t)!;
Every typeof() here compiles to a token. After renaming, PdfExporter is called a and the dictionary still returns the right Type, because the obfuscator rewrote the token when it renamed the row. The keys "pdf", "csv", "xlsx" are your vocabulary, not the runtime’s — renaming never looks at them, and you can even leave string encryption on them because their value is a contract you control, not a leaked identifier. You keep zero type names on your keep-list. That is the ideal outcome: full renaming, no readable holes, and it survives future refactors of the type names too.
When you genuinely cannot decouple — the type names are a published contract, or a host outside your assembly resolves them — keep exactly those names and nothing else. In nebula.config.json, a name-only keep sits above the rename catch-all:
{
"inputs": [ "bin/Release/net8.0/Acme.App.dll" ],
"encryptStrings": true,
"controlFlowObfuscation": true,
"renaming": {
"rules": [
{ "pattern": "Acme.Exporters.*Exporter", "action": "keep" },
{ "pattern": "Acme.*", "action": "rename" }
]
}
}
Or pin it at the declaration with an attribute, which keeps the rule next to the code it protects:
[Obfuscation(Feature = "renaming", Exclude = true)]
public sealed class PdfExporter : IExporter { /* ... */ }
Either way the keep is name-only: PdfExporter’s method bodies are still control-flow obfuscated and its string literals still encrypted. You have stopped its name from moving so the string can find it — you have not exempted its logic from protection. But remember the standing cost from the keep-list discipline: every kept name is a cleartext label in the metadata. A registry keyed on "pdf" leaks nothing; a kept PdfExporter leaks the class name. Prefer the registry wherever you own the resolution.
When it still bites
A few edges catch people even after they understand the mechanism:
- Namespace-only renames. Even
Type.GetType("Acme.Exporters.PdfExporter")with no assembly suffix breaks, because the namespace is renamed too. People sometimes assume only the leaf name matters. - Nested types. A nested type’s name in a string uses a
+separator (Outer+Inner). Renaming both parts breaks the string just the same, and the+form is easy to forget when you write a keep pattern. - Generic types. The reflection name of a generic carries a backtick arity (
List\1). If you storeMyCache`1` and rename it, the arity stays but the name moves — same failure. - Plugins in other assemblies. If the type lives in an assembly that is not part of this obfuscation run — a third-party plugin — your run never renames it, so its name is stable and
Type.GetTypekeeps working. The registry pattern still helps you, but the break only applies to types you yourself obfuscate. - Assembly name in the string. The assembly-identity portion (
, Acme.Exporters) is matched against the assembly name, which renaming does not change — so that half of the string is fine. It is always the type portion that drifts.
Takeaway
A metadata token moves with the rename; a string does not. Any time your own code names one of your own types as text — Type.GetType, a type name in config, a persisted discriminator — renaming silently invalidates it while leaving every test on the unobfuscated build green. The durable fix is not a keep-list entry; it is to resolve through typeof() and carry a key you chose, so the only thing that crosses the storage boundary is a word the runtime never has to match against a renamed name. See the companion pitfalls catalog for the other by-name failures, and Nebula.NET for the renaming and string-encryption passes described here.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.