MethodBody.Simplify() simplifies the body by turning short
instructions into normal ones (e.g. leave_s to leave)
After that, you can inject instructions without overflowing the
offsets.
Finally, you can use MethodBody.Optimize() to turn all into short form
when possible.
On Mar 18, 12:13 am, "
martin....@dotnetsharp.com"
<
martin....@dotnetsharp.com> wrote:
> //A method with try ... catch (MSIL) whose Assembly is loaded by
> Mono.Cecil and instructions injected in catch{ ... } block
> .method private hidebysig instance void TestMethod (object A,
> class [mscorlib]
> System.EventArgs a) cil managed
> {
> // Code size 544 (0x220)
> .maxstack 5
> .locals init (int32 V_0,
> int32 V_1,
> class [mscorlib]System.Exception V_2,
> int32[] V_3,
> string V_4)
> .try
> {
> IL_0000: ldc.i4.2
> IL_0001: stloc.0
> IL_0002: ldc.i4.3
> IL_0003: stloc.1
> IL_0004: leave.s IL_001f /*** should be IL_021f ***/
> } // end .try
> catch [mscorlib]System.Exception
> {
> IL_0006: stloc.2
> IL_0007: ldarg.0
>
> // The original instruction is a "ldstr" has been replaced with a
> list of instructions that I dreamed up
> //.......................