A refactoring from an if/else to a ternary expression may cause the AssemblyPatcher to emit invalid IL. The generated ldftn operand retains a metadata token from what I assume is the temporary Roslyn-compiled assembly. At runtime this leads to the following exception:
InvalidProgramException: Invalid IL code in Mods.CustomScenarios.StartScenarioWindow:Fill (): IL_0087: ldftn 0x06009c75
The issue can be reproduced with following code (example from CustomScenarios mod):
// **Following code works because we don't use ternaries:**
// if (localizedName == techName)
// scenarioItem.SetName(scenario.Name);
// else
// scenarioItem.SetName(localizedName);
// **This generates a runtime error:**
scenarioItem.SetName(localizedName == techName ? scenario.Name : localizedName);
A refactoring from an
if/elseto a ternary expression may cause the AssemblyPatcher to emit invalid IL. The generatedldftnoperand retains a metadata token from what I assume is the temporary Roslyn-compiled assembly. At runtime this leads to the following exception:The issue can be reproduced with following code (example from CustomScenarios mod):