# Renaming flows through your expression trees


You renamed every type and member, shipped the protected build, and your data layer kept working — queries ran, rows came back. Then a column quietly turned up named `a`. The lambda you pass your ORM, `o => o.Total`, still compiled and still ran; renaming did not break it. It did something subtler: it reached inside the expression tree and changed the name the ORM *reads back out* of it.

This is the part of renaming people miss. An expression-tree lambda is not code the way a delegate is — it is a little data structure your program builds at run time, and renaming rewrites the references inside it just like references anywhere else. That is exactly why the tree keeps resolving. It is also exactly why anything that mines a *name* out of the tree gets the new one.

## The real question

What does `o => o.Total` actually compile to when its type is `Expression<Func<Order, decimal>>`, and which parts of it does renaming touch? Answer that and the `a`-named column stops being a mystery.

## What the lambda compiles to

Take the simplest possible case:

```csharp
using System;
using System.Linq.Expressions;

public class Customer { public string Name { get; set; } = ""; }

public static class P
{
    public static Expression<Func<Customer, string>> E() => c => c.Name;
}
```

Assigned to a delegate, `c => c.Name` would be a method body that reads the property. Assigned to `Expression<Func<...>>`, the compiler emits something completely different: a recipe that calls the `Expression` factory methods to build the tree. Here is the actual IL for `E`:

```
.locals init ([0] class ...ParameterExpression)
IL_0000: ldtoken      Customer
IL_0005: call         class Type Type::GetTypeFromHandle(valuetype RuntimeTypeHandle)
IL_000a: ldstr        "c"
IL_000f: call         class ParameterExpression Expression::Parameter(class Type, string)
IL_0014: stloc.0
IL_0015: ldloc.0
IL_0016: ldtoken      method instance string Customer::get_Name()
IL_001b: call         class MethodBase MethodBase::GetMethodFromHandle(valuetype RuntimeMethodHandle)
IL_0020: castclass    [System.Runtime]System.Reflection.MethodInfo
IL_0025: call         class MemberExpression Expression::Property(class Expression, class MethodInfo)
IL_002a: ldc.i4.1
IL_002b: newarr       ParameterExpression
IL_0030: dup
IL_0031: ldc.i4.0
IL_0032: ldloc.0
IL_0033: stelem.ref
IL_0034: call         class Expression`1<...> Expression::Lambda<...>(class Expression, ParameterExpression[])
IL_0039: ret
```

Two lines decide everything about how renaming behaves here, and they are different in kind.

**The member access is a token.** `IL_0016: ldtoken method instance string Customer::get_Name()` loads a *metadata token* for the property's getter, which `GetMethodFromHandle` resolves to a `MethodInfo`, which `Expression.Property` turns into a `MemberExpression`. The getter is referenced the way every method reference in the assembly is referenced — by a metadata handle, not by its spelling.

**The parameter name is a string.** `IL_000a: ldstr "c"` pushes the literal `"c"` into `Expression.Parameter`. That string is the lambda parameter's display name. It is plain data in the `#US` heap, with no link to anything.

<figure class="my-8"><svg viewBox="0 0 780 430" role="img" aria-label="A lambda compiled to an expression tree encodes member access as a metadata token, which renaming rewrites, while the parameter name stays a plain string literal; at run time Member.Name returns the renamed name, so a derived column name changes."><defs><marker id="ah" markerWidth="9" markerHeight="9" refX="7" refY="3" orient="auto"><path d="M0,0 L7,3 L0,6 z" fill="#64748b"/></marker></defs><rect x="0" y="0" width="780" height="430" fill="none"/><text x="24" y="30" fill="#6366f1" font-size="16" font-weight="bold">c =&gt; c.Name built by factory calls, not a method body</text><rect x="24" y="52" width="350" height="104" rx="10" fill="#eef2ff" stroke="#6366f1" stroke-width="1.5"/><text x="40" y="78" fill="#6366f1" font-size="13.5" font-weight="bold">member access</text><text x="40" y="100" fill="#334155" font-size="12" font-family="monospace">Expression.Property(p, mi)</text><text x="40" y="120" fill="#0891b2" font-size="12" font-family="monospace">ldtoken get_Name</text><text x="40" y="142" fill="#64748b" font-size="11.5">metadata TOKEN, not a string</text><rect x="406" y="52" width="350" height="104" rx="10" fill="#eef2ff" stroke="#6366f1" stroke-width="1.5"/><text x="422" y="78" fill="#6366f1" font-size="13.5" font-weight="bold">parameter</text><text x="422" y="100" fill="#334155" font-size="12" font-family="monospace">Expression.Parameter(t, "c")</text><text x="422" y="120" fill="#0891b2" font-size="12" font-family="monospace">ldstr "c"</text><text x="422" y="142" fill="#64748b" font-size="11.5">string LITERAL, display name only</text><path d="M199,156 L199,190" stroke="#64748b" stroke-width="1.5" fill="none" marker-end="url(#ah)"/><path d="M581,156 L581,190" stroke="#64748b" stroke-width="1.5" fill="none" marker-end="url(#ah)"/><rect x="24" y="192" width="732" height="72" rx="10" fill="#ecfdf5" stroke="#10b981" stroke-width="1.5"/><text x="40" y="218" fill="#10b981" font-size="13.5" font-weight="bold">renamer pass — rewrites references, leaves literals</text><text x="40" y="242" fill="#334155" font-size="12" font-family="monospace">get_Name  →  get_a   (token target rewritten)</text><text x="470" y="242" fill="#64748b" font-size="11.5">"c" untouched (not a reference)</text><path d="M390,264 L390,300" stroke="#64748b" stroke-width="1.5" fill="none" marker-end="url(#ah)"/><rect x="24" y="302" width="732" height="104" rx="10" fill="#fff7ed" stroke="#d97706" stroke-width="1.5"/><text x="40" y="328" fill="#d97706" font-size="13.5" font-weight="bold">run time: the tree still resolves — but the derived NAME moved</text><text x="40" y="354" fill="#334155" font-size="12" font-family="monospace">lambda.Compile()(order)  →  reads the right property  ✓</text><text x="40" y="378" fill="#334155" font-size="12" font-family="monospace">((MemberExpression)body).Member.Name  →  "a"</text><text x="400" y="378" fill="#d97706" font-size="12" font-family="monospace">→ column "a", not "Total"</text></svg><figcaption class="text-sm text-muted mt-2">Member access is a metadata token that renaming rewrites with the member; the parameter name is an inert literal. The tree resolves correctly, but <code>Member.Name</code> now reads the renamed name.</figcaption></figure>

## Why renaming reaches the member but not the lambda's behaviour

Renaming rewrites *references*. The `ldtoken Customer::get_Name()` is a reference, so when the renamer turns `Name` into `a` — renaming the property and its `get_Name` accessor to `get_a` — it rewrites that token to point at `get_a`. The expression tree, rebuilt at run time, now holds a `MemberExpression` whose member is the renamed property. Compile the tree and invoke it and it reads the correct backing field. Nothing about execution breaks, because the tree never depended on the *text* `"Name"` to find the member — it held a handle.

The `ldstr "c"` is not a reference, so the renamer leaves it alone, which is harmless: the parameter's display name is cosmetic.

So where does `"a"` come from? From `Member.Name`. `MemberExpression.Member` is a `MemberInfo`, and `MemberInfo.Name` returns the member's *current* metadata name — after renaming, `"a"`. Any code that walks the tree to extract a name reads the renamed one:

```csharp
// A typical "derive the name from the lambda" helper — the pattern ORMs,
// validators, and mappers all use under the hood:
static string MemberName<T, TP>(Expression<Func<T, TP>> e) =>
    ((MemberExpression)e.Body).Member.Name;   // was "Total", now "a"

modelBuilder.Entity<Order>()
    .Property(o => o.Total)
    .HasColumnName(MemberName<Order, decimal>(o => o.Total)); // column "a"
```

The lambda compiled. The lambda ran. The *string mined out of it* changed — and that string was your column name, your JSON key, or the field name in a validation message.

## Contrast: the delegate form has nothing to break

Assign the same lambda to a delegate and the compiler emits an ordinary method body:

```
ldarg.0
callvirt   instance string Customer::get_Name()
ret
```

Renaming rewrites the `callvirt` target to `get_a`; the method runs fine; no name is derived from anything. The hazard is specific to the expression-tree form, because only that form turns the member access into data a library can inspect for its name.

## How to keep the contract stable

Two fixes, and the second is the one to prefer.

**Exclude the mapped members from renaming.** If a type's property names are the source of truth for an external contract, keep those names stable in [Nebula.NET](https://delta1labs.com/products/nebula) and let everything else rename:

```json
{
  "inputs": ["bin/Release/net8.0/Orders.dll"],
  "outputDirectory": "bin/Release/net8.0/obf",
  "renameIdentifiers": true,
  "encryptStrings": true,
  "excludeMembers": ["Order.Total", "Order.CustomerName", "Order.PlacedAt"]
}
```

Now `o => o.Total` yields `Member.Name == "Total"` after protection, and the derived column name is correct. The cost is that those property names stay readable — acceptable for a handful of persistence fields, and the rest of the type is still renamed.

**Pin the contract so it no longer depends on the name.** The deeper fix is to stop deriving the external string from the member name at all. Give the mapping an explicit name — `[Column("Total")]`, `.HasColumnName("Total")`, `[JsonPropertyName("total")]` — and the contract is fixed regardless of what the member is called. Then you can rename the property freely, because nothing reads its name to build the contract:

```csharp
modelBuilder.Entity<Order>()
    .Property(o => o.Total)
    .HasColumnName("Total");   // pinned — survives renaming, member can become "a"
```

This is strictly better protection: the property name is no longer load-bearing, so you can obfuscate it.

## Caveats

- **The parameter name leaks as a literal.** `ldstr "c"` is cosmetic, but a descriptive parameter name (`customerOrder`) sits in the `#US` heap in plaintext. Turn on `encryptStrings` if you care, though it discloses little.
- **Compiled-and-invoked trees are always fine.** If you only `.Compile()` and call the lambda, renaming is transparent — ignore all of this. The risk is exclusively name *derivation*.
- **Exclusions must be consistent across the graph.** If the lambda lives in one assembly and the entity in another, both must be protected together so the token rewrite stays coherent; a keep-list entry has to name the member in the assembly that actually defines it.
- **String encryption does not help here.** The name is a resolved `MemberInfo`, not a literal — encryption never touches it. Use exclusions or explicit mapping.

## Takeaway

Renaming is not blind to expression trees; it flows right through them, rewriting the member tokens inside so the tree still resolves and the lambda still runs. What moves is any name you *read back* out of the tree: `Member.Name` returns the live, renamed name, so ORMs, serializers, and validators that derive a string from `x => x.Prop` quietly start emitting the obfuscated one. Exclude the few members whose names are a contract, or — better — pin the contract with an explicit attribute or mapping so the member name carries no weight and you can obfuscate it like everything else.

