From ab9bd42251c0bae987d4bfd1578f83c196bebd37 Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Mon, 16 Dec 2019 15:38:26 +0100 Subject: [PATCH] Add index based method to ILProcessor This makes it simpler to work with it when the instruction positions are known in advance. --- Mono.Cecil.Cil/ILProcessor.cs | 27 +++++++++++++++++++++++ Test/Mono.Cecil.Tests/ILProcessorTests.cs | 24 ++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/Mono.Cecil.Cil/ILProcessor.cs b/Mono.Cecil.Cil/ILProcessor.cs index 039d2fc3a..a3fb8f10a 100644 --- a/Mono.Cecil.Cil/ILProcessor.cs +++ b/Mono.Cecil.Cil/ILProcessor.cs @@ -229,6 +229,16 @@ public void InsertAfter (Instruction target, Instruction instruction) instructions.Insert (index + 1, instruction); } + public void InsertAfter (int index, Instruction instruction) + { + if (index < 0 || index >= instructions.Count) + throw new ArgumentOutOfRangeException ("index"); + if (instruction == null) + throw new ArgumentNullException ("instruction"); + + instructions.Insert (index + 1, instruction); + } + public void Append (Instruction instruction) { if (instruction == null) @@ -248,6 +258,15 @@ public void Replace (Instruction target, Instruction instruction) Remove (target); } + public void Replace (int index, Instruction instruction) + { + if (instruction == null) + throw new ArgumentNullException ("instruction"); + + InsertAfter (index, instruction); + RemoveAt (index); + } + public void Remove (Instruction instruction) { if (instruction == null) @@ -256,5 +275,13 @@ public void Remove (Instruction instruction) if (!instructions.Remove (instruction)) throw new ArgumentOutOfRangeException ("instruction"); } + + public void RemoveAt (int index) + { + if (index < 0 || index >= instructions.Count) + throw new ArgumentOutOfRangeException ("index"); + + instructions.RemoveAt (index); + } } } diff --git a/Test/Mono.Cecil.Tests/ILProcessorTests.cs b/Test/Mono.Cecil.Tests/ILProcessorTests.cs index 3d9d1751b..f66d78c7f 100644 --- a/Test/Mono.Cecil.Tests/ILProcessorTests.cs +++ b/Test/Mono.Cecil.Tests/ILProcessorTests.cs @@ -53,6 +53,30 @@ public void InsertAfter () AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method); } + [Test] + public void InsertAfterUsingIndex () + { + var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3); + var il = method.GetILProcessor (); + + il.InsertAfter ( + 0, + il.Create (OpCodes.Ldloc_1)); + + AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method); + } + + [Test] + public void ReplaceUsingIndex () + { + var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3); + var il = method.GetILProcessor (); + + il.Replace (1, il.Create (OpCodes.Nop)); + + AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Nop, OpCodes.Ldloc_3 }, method); + } + static void AssertOpCodeSequence (OpCode [] expected, MethodBody body) { var opcodes = body.Instructions.Select (i => i.OpCode).ToArray ();