How renaming breaks MVVM Toolkit bindings
CommunityToolkit.Mvvm turns a [ObservableProperty] field into a public property and a [RelayCommand] method into a command, and XAML binds to those generated names by string. This post reads the generated IL to show which names a binding resolves as a string — and must be kept — versus the ones the generator wired as a direct delegate or token, which rename for free. Then it scopes Nebula to keep the former and obfuscate everything else.
A CommunityToolkit.Mvvm view-model is mostly code you never wrote. You declare a field and an attribute; a source generator produces the public property, the PropertyChanged plumbing and the command object at compile time. That is the appeal — and it is also why obfuscating such an app trips people up. The names XAML binds to are not in your source. They are generated, they are public, and some of them are resolved by string at runtime while others only look like they are.
The real question
When you rename an assembly, the honest question is never “will binding break” — it is “which of these generated names does the runtime look up by its string, and which did the generator already wire as a direct reference?” The first set must be kept. The second renames with no effect. Get that distinction right and you obfuscate an MVVM Toolkit app as hard as any other; get it wrong and you either leak the whole view-model surface or ship dead controls. The distinction is visible in the generated IL, so let us read it.
What the generator produces
Start with a small view-model written the MVVM Toolkit way.
public partial class CustomerViewModel : ObservableObject
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(DisplayName))]
private string _firstName = "";
public string DisplayName => $"Customer: {FirstName}";
[RelayCommand(CanExecute = nameof(CanSave))]
private void Save() { /* persist */ }
private bool CanSave() => FirstName.Length > 0;
}
The generator emits a partial class completing this. Trimmed to the parts that matter, the generated property looks like this:
partial class CustomerViewModel
{
public string FirstName
{
get => _firstName;
set
{
if (!EqualityComparer<string>.Default.Equals(_firstName, value))
{
OnPropertyChanging("FirstName");
_firstName = value;
OnPropertyChanged("FirstName");
OnPropertyChanged("DisplayName"); // from NotifyPropertyChangedFor
}
}
}
private RelayCommand? saveCommand;
public IRelayCommand SaveCommand =>
saveCommand ??= new RelayCommand(Save, CanSave); // direct delegates
}
(The real generator routes the change notifications through cached PropertyChangedEventArgs instances for allocation reasons, but the string content is exactly what you see.) Two kinds of name now live in this IL, and they behave completely differently under renaming.
Strings versus tokens
The getter and setter reach the backing field with ldfld/stfld against a field token:
IL_0001: ldarg.0
IL_0002: ldfld string MyApp.CustomerViewModel::_firstName
That token points at the FieldDef row, not at the text _firstName. Rename the field to a and Nebula rewrites the row; the ldfld still resolves. The field name is free.
The notification, by contrast, carries a genuine string constant:
IL_0014: ldarg.0
IL_0015: ldstr "FirstName"
IL_001a: call instance void ...ObservableObject::OnPropertyChanged(string)
That ldstr "FirstName" is the name WPF’s binding engine will try to match. When XAML says {Binding FirstName}, the engine walks the DataContext type’s properties by name through PropertyDescriptor/reflection, finds a property literally called FirstName, subscribes to PropertyChanged, and filters notifications by comparing the event’s PropertyName string. Rename the property to a and three things go wrong at once: the path lookup finds nothing, the binding reports a path error and falls back to a default, and the OnPropertyChanged("FirstName") string no longer names anything the UI is listening for. All silently.
The command is the same story with one extra wrinkle. SaveCommand is a public property, so {Binding SaveCommand} resolves it by string exactly like FirstName — keep it. But the CanSave predicate inside it is a delegate, passed as new RelayCommand(Save, CanSave). The generator resolved nameof(CanSave) at compile time and emitted a direct method reference; there is no "CanSave" string anywhere at runtime. CanSave renames for free, and so does Save. The only command-related name that faces the binding layer is SaveCommand itself.
So the keep-list writes itself: the generated public properties and commands on every view-model, plus any property named inside a [NotifyPropertyChangedFor] (its nameof becomes a runtime ldstr). Everything else the generator produced — backing fields, CanExecute predicates, command target methods, the OnXxxChanged/OnXxxChanging partial hooks — is referenced by token or delegate and renames with no effect on binding.
Scoping Nebula
Because the generated members you must keep are exactly the public surface of your view-models, a single members-scoped keep rule expresses it precisely.
{
"schemaVersion": 1,
"inputs": ["bin/Release/net8.0/MyApp.dll"],
"outputDirectory": "protected",
"encryptStrings": true,
"controlFlowObfuscation": true,
"renaming": {
"rules": [
{ "pattern": "MyApp.ViewModels.*", "action": "keep", "members": "public" },
{ "pattern": "MyApp.*", "action": "rename" }
]
}
}
The first rule keeps the public property and command names on every view-model — the generated FirstName, DisplayName and SaveCommand — and the members: "public" qualifier lets the private _firstName field, the CanSave predicate, the Save method and the partial hooks rename normally. The second rule renames everything else in the assembly: your services, your persistence layer, your domain logic. Crucially, a keep is name-only — the kept view-models still get their method bodies control-flow-obfuscated and their string literals encrypted. You give up renaming on a handful of binding-facing names, not protection on the code behind them.
If you would rather not reason about which namespace holds view-models, preservePublicApi: true keeps the whole externally visible surface and achieves the same thing for any view-model that is public — at the cost of also keeping every other public type’s names. For a shipped desktop app with no library consumers, the namespace-scoped rule above leaks less.
Caveats and when it breaks
The failure mode is the quiet one. A renamed bound property does not throw — WPF and WinUI log a BindingExpression path error to the debug trace and leave the control at its fallback value. A smoke test that merely launches the app and sees a window will pass while half the fields are blank. The only honest acceptance test is to run the protected build and exercise the bound screens, watching the debug output for a path error that names the member that got renamed.
Two edges are worth stating. First, if a view-model is internal rather than public — some teams keep them internal and expose them only through an interface — then members: "public" does not cover it and preservePublicApi does not either; the generated properties are still public members of a non-public type, and you must keep that view-model explicitly. Second, [ObservableProperty] applied to a field whose name feeds a computed property via [NotifyPropertyChangedFor] creates a dependency you can miss: the computed DisplayName must be kept too, because its name travels as a runtime string in the setter and the UI binds to it. Keeping the whole view-model’s public members, as above, covers both without you having to enumerate them.
And the ground rule underneath all of it: this is about protecting your own application’s IP. Keep the names the binding layer resolves by string, obfuscate the logic those bindings sit in front of.
Takeaway
The MVVM Toolkit generator emits two kinds of name into your assembly, and renaming treats them oppositely. The names that face the binding layer — the generated public properties, the commands, and any property raised through [NotifyPropertyChangedFor] — live as runtime strings and must survive. The names wired as metadata tokens or direct delegates — backing fields, CanExecute predicates, command targets, partial hooks — rename for free. A members: "public" keep on your view-model namespace draws exactly that line, which is why obfuscating a CommunityToolkit.Mvvm app is a scoping problem, not a reason to avoid obfuscation. Nebula.NET keeps the binding contract and obfuscates everything behind it; open the result in Glass.NET to confirm your view-models’ public names survived while the internals read as a, b, c.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.