Skip to content

Instantly share code, notes, and snippets.

@einarwh
Last active December 27, 2015 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/7295906 to your computer and use it in GitHub Desktop.
Save einarwh/7295906 to your computer and use it in GitHub Desktop.
private void TamperWith(
MethodDefinition method,
IEnumerable<Instruction> calls)
{
foreach (var call in calls)
{
var il = method.Body.GetILProcessor();
int counter = method.Parameters.Count;
while (counter > 0)
{
var starg = il.Create(OpCodes.Starg, --counter);
il.InsertBefore(call, starg);
}
var start = method.Body.Instructions[0];
var loop = il.Create(OpCodes.Br_S, start);
il.InsertBefore(call, loop);
il.Remove(call.Next); // Ret
il.Remove(call);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment