private static void InsertInvalidIl(MethodBody methodBody)
{
//Get the instructions and cil worker
var instructions = methodBody.Instructions;
if (instructions.Count <= 0)
{
return; //We can only do this if we have instructions to work with
}
ILProcessor il = methodBody.GetILProcessor();
//First create an invalid il instruction
OpCode fakeOpCode = CreateInvalidOpCode();
Instruction invalidIlInstr1 = il.Create(fakeOpCode);
Instruction invalidIlInstr2 = il.Create(fakeOpCode);
Instruction originalFirst = instructions[0];
//Insert invalid il at the start
il.InsertBefore(originalFirst, invalidIlInstr1);
il.InsertBefore(invalidIlInstr1, invalidIlInstr2);
//Create the branch statement
Instruction branchStatement = il.Create(OpCodes.Br_S, originalFirst);
//Add the branch to the start
il.InsertBefore(invalidIlInstr2, branchStatement);
//Readjust the offsets
il.AdjustOffsets(methodBody, 4);
}