/// <summary> /// Break the method instructions in to section based on 'Pop' and 'Stloc' instructions. /// </summary> private List <List <ILInstruction> > GetMethodSections(MethodBodyReader reader) { List <List <ILInstruction> > sections = new List <List <ILInstruction> >(); List <ILInstruction> section = new List <ILInstruction>(); ILInstruction previous = null; int stack = 0; foreach (ILInstruction instruction in reader) { stack += reader.GetStackDelta(instruction); // The return statement is replaced later, so don't add it here. if (instruction.Code != OpCodes.Ret) { section.Add(instruction); } if (IsSectionEnd(instruction, previous) && stack == 0) { sections.Add(section); section = new List <ILInstruction>(); } previous = instruction; } if (section.Count > 0) { sections.Add(section); } return(sections); }