private static void NopRet(Mono.Cecil.Cil.MethodBody methodBody) { if (methodBody == null) { return; } var ilProcessor = methodBody.GetILProcessor(); if (ilProcessor == null) { return; } foreach (var instruction in methodBody.Instructions.ToArray()) { ilProcessor.Remove(instruction); } ilProcessor.Append(ilProcessor.Create(OpCodes.Nop)); ilProcessor.Append(ilProcessor.Create(OpCodes.Ret)); }
public static void CopyBlock(Mono.Cecil.Cil.MethodBody body, int fromIndex, int startIndex, int endIndex) { //copy instructions startIndex to endIndex after fromIndex if (fromIndex >= startIndex - 1 && fromIndex <= endIndex) return; Collection<Instruction> instructions = body.Instructions; Type icType = typeof(Collection<Instruction>); Instruction[] alIns = (Instruction[])icType.InvokeMember("items", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, instructions, null); int copyCount = endIndex - startIndex + 1; Instruction[] al = new Instruction[instructions.Count + copyCount]; int curIndex = 0; //0 to fromIndex int count = fromIndex + 1; Array.Copy(alIns, 0, al, curIndex, count); curIndex += count; //startIndex to endIndex count = copyCount; ILProcessor ilp = body.GetILProcessor(); for (int i = 0, j = curIndex; i < count; i++, j++) { Instruction ins = instructions[i + startIndex]; al[j] = InsUtils.CreateInstruction(ins.OpCode, ins.Operand); } curIndex += count; //fromIndex+1 to end count = instructions.Count - fromIndex - 1; Array.Copy(alIns, fromIndex + 1, al, curIndex, count); curIndex += count; if (instructions.Count + copyCount != curIndex) { throw new ApplicationException("Internal CopyBlock error!"); } al[fromIndex].Next = al[fromIndex + 1]; int index; for (int i = 0; i < copyCount; i++) { index = i + fromIndex + 1; al[index].Previous = al[index - 1]; al[index].Next = (index < al.Length - 1 ? al[index + 1] : null); } index = fromIndex + copyCount + 1; if (index < al.Length) { al[index].Previous = al[index - 1]; } alIns = null; while (instructions.Count < al.Length) { instructions.Add(InsUtils.CreateInstruction(OpCodes.Nop, null)); } alIns = (Instruction[])icType.InvokeMember("items", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, instructions, null); Array.Copy(al, alIns, al.Length); instructions[instructions.Count - 1].Next = null; icType.InvokeMember("size", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance, null, instructions, new object[] { al.Length }); InsUtils.ComputeOffsets(instructions); }