Yes :)

I suggest you put a break point on the line:

   MethodDef.Body.Variables.Add(VariableDef);

And step through your program.

You wil start with the foreach, which iterates over each instruction.

Pay attention to what happens if you find one : you'll execute the replace
many times, that's probably not what you intend to do.

Jb

If the instruction is a ldc

On Sat, Oct 25, 2014 at 12:09 AM, <[email protected]>
wrote:

> Yep, the array creation and initialization worked like a charm now.
> Still, I havent got an answer for the replacment of ldc.i4's with the
> fitting array element.
> My attemp was this, its giving me an out of range exception.. :c
>
> if (IsMutatable(MethodDef))
>                             {
>                                 VariableDefinition VariableDef = new
> VariableDefinition(string.Format("myarr_{0}", rnd.Next(1, int.MaxValue)),
> TypeDef.Module.Import(typeof(int)));
>                                 ILProcessor ilp =
> MethodDef.Body.GetILProcessor();
>                                 int IntegerAmount =
> CountIntegers(MethodDef);
>
>                                 // Check if method has a body
>                                 if (MethodDef.HasBody)
>                                 {
>                                     int[] TestArray = new int[] { 1, 2, 3,
> 4, 120, 200, 1, 5, 7, 2, 500 };
>                                     List<Instruction> InstructionList =
> CreateMSILArray(TypeDef, VariableDef, TestArray);
>                                     InstructionList.Reverse();
>                                     foreach (Instruction ArrayInstruction
> in InstructionList)
>                                     {
>
> MethodDef.Body.Instructions.Insert(0, ArrayInstruction);
>                                     }
>
> MethodDef.Body.Variables.Add(VariableDef);
>
>                                     // Replace Integers with Array Element
> Assignment
>
>                                     foreach (Instruction InstructionDef in
> MethodDef.Body.Instructions)
>                                     {
>                                         if (InstructionDef.OpCode ==
> OpCodes.Ldc_I4)
>                                         {
>                                             for (int i = 0; i <
> InstructionList.Count; i++)
>                                             {
>                                                 //if
> ((int)InstructionDef.Operand == (int)InstructionList[i].Operand )
>                                                 //{
>                                                 var newInstruction =
> Instruction.Create(OpCodes.Ldloc_0);
>
> ilp.Replace(InstructionDef, newInstruction);
>
> ilp.InsertAfter(newInstruction, Instruction.Create(OpCodes.Ldc_I4, i));
>
> ilp.InsertAfter(newInstruction.Next, Instruction.Create(OpCodes.Ldelem_I4));
>
> Console.WriteLine("Replaced one integer with array element.");
>                                                 //  }
>                                             }
>                                         }
>                                     }
>
>                                 }
>
>
> <http://puu.sh/cpcm8/d885cdde01.png>
> The problem must be somewhere here, where i try to replace the ldc.i4 with
> the array element.
> Do you have an idea why that is happening? :/
>
>
>  --
> --
> --
> 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