diff --git a/ICSharpCode.Decompiler/IL/Transforms/TransformDisplayClassUsage.cs b/ICSharpCode.Decompiler/IL/Transforms/TransformDisplayClassUsage.cs index 5add9da473..a6d649756a 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/TransformDisplayClassUsage.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/TransformDisplayClassUsage.cs @@ -652,9 +652,16 @@ DisplayClass HandleMonoStateMachine(ILFunction function, ILVariable thisVariable .FirstOrDefault(t => t.IsThis() && t.Type.GetDefinition() == decompilationContext.CurrentTypeDefinition); if (thisVar == null) { - thisVar = new ILVariable(VariableKind.Parameter, decompilationContext.CurrentTypeDefinition, -1) { Name = "this" }; + thisVar = new ILVariable(VariableKind.Parameter, decompilationContext.CurrentTypeDefinition, -1) { + Name = "this", StateMachineField = thisField + }; function.Variables.Add(thisVar); } + else if (thisVar.StateMachineField != null && displayClass.VariablesToDeclare.ContainsKey(thisVar.StateMachineField)) + { + // "this" was already added previously, no need to add it twice. + return displayClass; + } VariableToDeclare variableToDeclare = new VariableToDeclare(displayClass, thisField, thisVar); displayClass.VariablesToDeclare.Add(thisField, variableToDeclare); }