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:
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.
Member.Name now reads the renamed name.
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:
// 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 and let everything else rename:
{
"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:
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#USheap in plaintext. Turn onencryptStringsif 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.
Top comments (0)