DEV Community

Why nameof leaves no trace to decompile

Write throw new ArgumentNullException(nameof(value)), compile it, and then decompile the assembly. The nameof is gone.

Not renamed, not obfuscated - gone, in the sense that there is nothing left in the file that was ever different from the plain string "value". This is not a decompiler limitation Glass could fix with a better algorithm. It is a fact about what the C# compiler chose to emit, and it marks one of the hardest, cleanest boundaries in all of decompilation: the line between a construct that leaves evidence and one that leaves a result.

The real question

Decompilation is a recovery problem. The compiler lowered your source to IL and metadata, and the decompiler runs that transformation backward. It works as well as it does because most of C# leaves evidence - a typeof leaves a token that names a type, a foreach leaves a recognizable call pattern, a lock leaves a try/finally around Monitor.Enter. The decompiler matches the shape and rebuilds the source.

So the question for nameof is simply: what evidence does it leave? And the uncomfortable answer is none. nameof(value) does not lower to a shape. It lowers to a value - the string "value" - and that value is indistinguishable from any other string with the same characters. There is nothing to match on.

The mechanism, shown

Put the two forms side by side in one method:

static void Check(object value) { string a = nameof(value); string b = "value"; }

The C# compiler emits this:

ldstr "value"
stloc.0
ldstr "value"
stloc.1
ret

Two ldstr instructions, identical operands. The first came from nameof(value); the second from the literal "value". Nothing in the instruction stream tells them apart. There is no custom modifier on the local, no attribute on the method, no entry in any metadata table that records "slot 0 was a nameof."

The reason is in the C# language specification: nameof is a constant expression of type string. The compiler is required to evaluate it at compile time and substitute the resulting constant - the same way it folds 2 + 2 to 4. By the time the bytes are written, a nameof is as absent from the program as the + in a folded arithmetic constant.

It gets one notch more total. The ldstr operand is not even the string - it is a token into the #US (user-string) heap, where string literals live. The compiler deduplicates that heap. So nameof(value) and "value" do not merely compile to the same instruction; they compile to the same 0x70 token pointing at the same single entry in #US. There is physically one copy of "value" in the file, and both source expressions point at it.

Both source forms fold to one constant, one ldstr, one deduplicated #US entry. The decompiler sees a single string with no record of its origin, so the faithful output is the literal.

Contrast this with typeof, which decompiles exactly. typeof(Customer) lowers to ldtoken plus a call to Type.GetTypeFromHandle, and the ldtoken operand is a metadata token that names the type. The evidence survives: the shape (the instruction pair) and the content (the token) are both still there.

nameof keeps neither. It has no distinctive shape - a single ldstr is the most common instruction in most assemblies - and no distinctive content, because the content is a plain string that any literal could also have produced.

The line this draws, precisely

The useful lesson is not "nameof is lost." It is why it is lost, because the same reasoning sorts every construct in the language into recoverable and unrecoverable. A construct is recoverable when the compiler stores it as structured metadata: a token, a typed blob, a table row. It is unrecoverable when the compiler folds it into a plain value that is indistinguishable from other sources of the same value.

By that test, [CallerMemberName] lands on exactly the same side as nameof. Write this:

void Raise([CallerMemberName] string name = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
// at the call site, inside a property setter named Total:
Raise();

The attribute lives on the parameter in metadata - that part is preserved - but its effect is at the call site, and there the compiler simply bakes in a literal:

ldstr "Total"
call void MyType::Raise(string)

The call site shows ldstr "Total" with no trace of [CallerMemberName], no trace of the defaulted argument, nothing. A decompiler reading only that method sees a bare string being passed and will - correctly, faithfully - render Raise("Total"). The compiler baked a string; the decompiler cannot un-bake it. Same category as nameof: the source form was consumed to produce a value.

Now the other side of the line. A custom attribute argument looks superficially similar - it is "just data" - but it is stored completely differently:

[Obsolete("use TryParse", true)] static int Parse(string s) => int.Parse(s);

Here the arguments "use TryParse" and true are serialized into the #Blob heap as a typed custom-attribute value blob: a prolog, then each argument tagged with its element type. The blob records that the first argument is a string and the second a bool. That structure is evidence. Glass reads the blob, knows the kinds, and reconstructs [Obsolete("use TryParse", true)] exactly - including the fact that true is a boolean, not the string "true". An enum argument keeps its underlying constant and its enum type reference; a typeof argument keeps its token. All of it survives because the compiler chose to store it structurally instead of folding it into an opaque value.

That is the whole boundary. nameof and [CallerMemberName] bake strings; attribute arguments, typeof, and enum constants keep structure. Recovery is possible precisely when, and only when, structure survives.

How Glass handles it

Glass decompiles the ldstr to the string literal. That is the faithful answer and the default, because it is the only output that is provably true to the IL. Rendering nameof(value) instead would be a claim the file does not support.

Glass can optionally annotate a string literal as a candidate nameof when the literal exactly matches an identifier that is in scope and used nearby in a recognized pattern - the argument to ArgumentNullException.ThrowIfNull, an OnPropertyChanged call, a logging scope key. When you enable that, Glass shows the literal and attaches a note that it could have been nameof. It never silently rewrites the string into nameof, and the note is always marked as a heuristic.

The distinction matters because the heuristic is a guess about the source, not a recovery of it. A literal that happens to equal an identifier is not necessarily a nameof - throw new ArgumentException("value") where "value" is a human-readable message that coincides with a parameter name is a real and common case, and rewriting it to nameof(value) would be wrong. Being wrong here is not a cosmetic slip; it changes what the reader believes the author wrote. So Glass draws the faithful string by default and treats any nameof rendering as an annotation you opt into, never as a fact it recovered.

You can read more about Glass and its decompilation model at /products/glass and in the docs.

When it breaks - and it is not Glass that breaks

The honest framing is that there is no fix. No decompiler, however good, can distinguish nameof(value) from "value", because the information does not exist in the file. This is not a gap in Glass's pattern library that a future release closes. It is a one-way transformation the compiler performed, and the only artifact that could tell you the source form - the source - is exactly what you do not have.

The practical consequences are small but worth knowing. If you decompile a library to study how it validates arguments, the nameof calls you see in the original source will read back as string literals (possibly annotated). Refactoring tools that rely on nameof to keep a string in sync with an identifier give you no signal in decompiled code, because the link is gone. And if you are comparing two builds to see what changed, a literal and a nameof of the same text are - correctly - identical, so a source edit from one to the other is invisible in the IL. In every case the right mental model is the same: you are looking at the result of nameof, and the operation that produced it left no return address.

Takeaway

nameof(x) is a compile-time constant. It evaluates to the string "x" and emits a single ldstr - the same token, pointing at the same deduplicated #US entry, that the literal "x" produces. Nothing distinguishes them, so a faithful decompiler shows the literal and treats any nameof rendering as a heuristic annotation, never a recovery.

The deeper lesson is the boundary it reveals: a construct is recoverable exactly when the compiler stored it as structure - a token, a typed blob, a table row - and unrecoverable when it folded it into a value. nameof and [CallerMemberName] fold; typeof and attribute arguments keep structure. Learn to read which side of that line a construct falls on, and you will always know how much to trust what the decompiler shows you.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.