diff --git a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs index 56fdfb6e9..91ca8d309 100644 --- a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs @@ -153,7 +153,7 @@ namespace ICSharpCode.Decompiler.CSharp switch (section.Body) { case Branch br: // we can only inline the block, if all branches are in the switchContainer. - if (br.TargetBlock.Parent == switchContainer && switchContainer.Descendants.OfType().Where(b => b.TargetBlock == br.TargetBlock).All(b => BlockContainer.FindClosestContainer(b) == switchContainer)) + if (br.TargetBlock.Parent == switchContainer && switchContainer.Descendants.OfType().Where(b => b.TargetBlock == br.TargetBlock).All(b => BlockContainer.FindClosestSwitchContainer(b) == switchContainer)) caseLabelMapping.Add(br.TargetBlock, firstValueResolveResult); break; default: @@ -167,7 +167,7 @@ namespace ICSharpCode.Decompiler.CSharp switch (section.Body) { case Branch br: // we can only inline the block, if all branches are in the switchContainer. - if (br.TargetBlock.Parent == switchContainer && switchContainer.Descendants.OfType().Where(b => b.TargetBlock == br.TargetBlock).All(b => BlockContainer.FindClosestContainer(b) == switchContainer)) + if (br.TargetBlock.Parent == switchContainer && switchContainer.Descendants.OfType().Where(b => b.TargetBlock == br.TargetBlock).All(b => BlockContainer.FindClosestSwitchContainer(b) == switchContainer)) ConvertSwitchSectionBody(astSection, br.TargetBlock); else ConvertSwitchSectionBody(astSection, section.Body); diff --git a/ICSharpCode.Decompiler/IL/Instructions/BlockContainer.cs b/ICSharpCode.Decompiler/IL/Instructions/BlockContainer.cs index c48021ac7..b70056841 100644 --- a/ICSharpCode.Decompiler/IL/Instructions/BlockContainer.cs +++ b/ICSharpCode.Decompiler/IL/Instructions/BlockContainer.cs @@ -226,5 +226,15 @@ namespace ICSharpCode.Decompiler.IL } return null; } + + public static BlockContainer FindClosestSwitchContainer(ILInstruction inst) + { + while (inst != null) { + if (inst is BlockContainer bc && bc.entryPoint.Instructions.FirstOrDefault() is SwitchInstruction) + return bc; + inst = inst.Parent; + } + return null; + } } }