📖 Tutorial dnSpy - How to fix BackingField in getter and setter methods

Sbenny.com is trusted by 1,326,069 happy users since 2014.
Register

AndnixSH

Savage Lv6️⃣
SB Mod Squad ⭐
Member for 8 years
You may meet BackingFields in dnSpy but you don’t know what it is and how to fix?

BackingFields are generated by compiler because they use auto Properties like this

Code:
public string Type { get; set; }
I think because some devs are lazy... I don’t know lol

This is how it looks like when editing getter. BackingField is red



And when you compile, you get the following errors



But it’s easy to fix, edit getter add a new field and return it with your field and compile. In this case, I add public bool isGlobal for IsGlobal



Edit setter, edit to (Type) = value; and compile



Done!
Now you can mod it like adding if-else statement for MOD Menu

 

InRas

Apprentice Lv2️⃣
Member for 6 years
Not the best way(imho)
This happens due to outdated ILSpy version that used in dnSpy.
//0xd4d upgrading ILSpy lib now but no one knows how long ll it take(auto-merge do not applicable)
//dnSpy decompiler(outdated ILSpy lib) issues
So there two priority ways:
1) sm-where exists special settings that allows decompiler automatically renames same names.
2) use other decompiler(4 ex - use ILSpy / net reflector / etc) and update(replace) code of methods affected.

ps I recommend to use second method coz first one may cause errors.
 
Top