Hey,

For one instruction, you're writing:

* Insert an instruction before it,
* Replace it with another instruction (thus removing it),
* Insert an instruction after it.

But the instruction has been removed, so you can't insert an instruction
after it.

Jb



On Mon, Oct 13, 2014 at 1:51 PM, <[email protected]>
wrote:

> Hello,
>
> I am trying to replace a ldstr instruction with a local variable that
> should hold the string value and then be replaced with the ldstr
> instruction.
>
> I tried this, but I can't get it to work:
>
> private static void InsertLocal(TypeDefinition TypeDef, MethodDefinition
> MethodDef, ILProcessor Ilp)
>         {
>
>             foreach (Instruction Instr in MethodDef.Body.Instructions)
>             {
>                 if (Instr.OpCode == OpCodes.Ldstr)
>                 {
>                     VariableDefinition VariableDef = new
> VariableDefinition(string.Format("num_{0}", rnd.Next(1, int.MaxValue)),
> TypeDef.Module.Import(typeof(string)));
>                     MethodDef.Body.Variables.Add(VariableDef);
>
>                     Ilp.InsertBefore(Instr,
> Instruction.Create(OpCodes.Ldstr, Instr.Operand.ToString()));
>                     Ilp.Replace(Instr, Instruction.Create(OpCodes.Stloc_S,
> VariableDef));
>                     Ilp.InsertAfter(Instr,
> Instruction.Create(OpCodes.Ldloc_S, VariableDef));
>                     Console.WriteLine("Replaced a string");
>                 }
>             }
>         }
>
> This produces a OutOfRange Exception:
>
>
> <http://puu.sh/car3q/b4e0e74723.png>
>
>
> Can someone help me out?
>
>  --
> --
> --
> mono-cecil
> ---
> You received this message because you are subscribed to the Google Groups
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
-- 
--
mono-cecil
--- 
You received this message because you are subscribed to the Google Groups 
"mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to