that works !! :) var vDef = (VariableDefinition)ins.Operand; srcIns = Instruction.Create(ins.OpCode,newMethod.Body.Variables[vDef.Index]);
I was wondering,when I checked the VariableDefinitionCollection in the new method body using break point all variables had an index 0 and named V_0 ;the method information was also not available. Thanks JB. 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
