Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJb Evain <jb@evain.net>2019-12-16 21:02:42 +0300
committerGitHub <noreply@github.com>2019-12-16 21:02:42 +0300
commitdaadf7659817104634be6d2ab30d1ff877fc11f6 (patch)
treecafcb88389395a564c4b808b01ee7a5a83e28c79
parent96026325ee1cb6627a3e4a32b924ab2905f02553 (diff)
parentab9bd42251c0bae987d4bfd1578f83c196bebd37 (diff)
Merge pull request #636 from marek-safar/api
Add index based method to ILProcessor
-rw-r--r--Mono.Cecil.Cil/ILProcessor.cs27
-rw-r--r--Test/Mono.Cecil.Tests/ILProcessorTests.cs24
2 files changed, 51 insertions, 0 deletions
diff --git a/Mono.Cecil.Cil/ILProcessor.cs b/Mono.Cecil.Cil/ILProcessor.cs
index 039d2fc..a3fb8f1 100644
--- a/Mono.Cecil.Cil/ILProcessor.cs
+++ b/Mono.Cecil.Cil/ILProcessor.cs
@@ -229,6 +229,16 @@ namespace Mono.Cecil.Cil {
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 @@ namespace Mono.Cecil.Cil {
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 @@ namespace Mono.Cecil.Cil {
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 3d9d175..f66d78c 100644
--- a/Test/Mono.Cecil.Tests/ILProcessorTests.cs
+++ b/Test/Mono.Cecil.Tests/ILProcessorTests.cs
@@ -53,6 +53,30 @@ namespace Mono.Cecil.Tests {
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 ();