:) There's a logic error in your code. Hint, try replacing your foreach/getPos contraption with a simple for loop.
Jb On Fri, Oct 24, 2014 at 5:41 PM, <[email protected]> wrote: > Hello, > > I have 2 questions. > > 1) I try to insert a integer array at the top of a method, which works... > kind of, but not properly: > > I do it like this: > > int[] test = new int[] { 1, 5, 3, 6, 9, 5 }; >> >> Instruction prev = null; >> List<Instruction> lst = >> CreateSILArray(TypeDef, VariableDef, test); >> lst.Reverse(); >> foreach (Instruction ins in lst) >> { >> MethodDef.Body.Instructions.Insert(0, >> ins); >> } >> >> >> // -------------------------------- >> >> private static List<Instruction> CreateSILArray(TypeDefinition DataType, >> VariableDefinition VariableDef, int[] values) >> { >> List<Instruction> il = new List<Instruction>(); >> >> il.Add(Instruction.Create(OpCodes.Ldc_I4, values.Length)); >> il.Add(Instruction.Create(OpCodes.Newarr, >> DataType.Module.Import(typeof(Int32)))); >> il.Add(Instruction.Create(OpCodes.Stloc, VariableDef)); >> >> foreach (int j in values) >> { >> int pos = getPos((int[])values, j); >> il.Add(Instruction.Create(OpCodes.Ldloc, VariableDef)); >> il.Add(Instruction.Create(OpCodes.Ldc_I4, pos)); >> il.Add(Instruction.Create(OpCodes.Ldc_I4, j)); >> il.Add((Instruction.Create(OpCodes.Stelem_I4))); >> } >> >> return il; >> >> } >> private static int getPos(int[] items, int item) >> { >> return Array.FindIndex(items, w => w == item); >> } >> >> >> > <http://puu.sh/coKTb/fd51770276.png> > > But you can see that the last item of the array is added again, index 1. > Any idea why that is, why its not creating an num[5] = 5? > > > 2) How would I call the element of an array, when i like to replace an > integer (ldc.i4) with a call to an array element? > > ~SDIM > > -- > -- > -- > 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.
