Thanks JB. that works !! :) var vDef = (VariableDefinition)ins.Operand; srcIns = Instruction.Create(ins.OpCode,newMethod.Body.Variables[vDef.Index]);
I was wondering,why the attributes of the VariableDefinition didn't appear in the new method in the same loop.The index of all the variables was 0.and the method infromation was also null. On May 19, 3:28 pm, Jb Evain <[email protected]> wrote: > Hi, > > On Thu, May 19, 2011 at 12:21 PM, srcKode <[email protected]> wrote: > > Can I get the Variables from the new method body with out actually > > saving it or adding it to the target assembly. > > Yes. > > > The steps i did to clone the method is > > > 1.Add parameters > > 2.Add Variables > > 3.Add Exception Handlers(this have some issues too) > > 4.Add Instructions > > > Sequentially ... > > In pseudo code: > > MethodDefinition originalMethod = ...; > MethodDefinition targetMethod = ...; > > foreach (VariableDefinition variable in originalMethod.Body.Variables) > targetMethod.Body.Variables.Add (new VariableDefinition (import > (variable.VariableType))); > > var il = targetMethod.Body.GetILProcessor (); > > foreach (var instruction in originMethod.Body.Variables) > switch (instruction.OpCode.OperandType) > case InlineVar: case ShortInlineVar: > var originalVar = (VariableDefinition) instruction.Operand; > il.Emit (instruction.OpCode, targetMethod.Body.Variables > [originalVar.Index]); > > Jb -- -- mono-cecil
