You have to analyze the whole body of the method to determine its exit points and track the stack usage to be sure to not mess up with it.
On Sun, Nov 21, 2010 at 3:48 AM, Jeff Nevins <[email protected]> wrote: > Anyone?....please :) > > On Nov 18, 4:22 pm, Jeff Nevins <[email protected]> wrote: >> I guess this is more of an IL instruction but I'd appreciate any help >> all the same. >> >> I need to add a method call at the end of an arbitrary set of methods. >> The Instructions I'm emitting are fine for most methods - I'm adding >> my new Instructions before the last found instruction with a Ret code: >> >> var returnInstruction = >> method.Body.Instructions.LastOrDefault(i => i.OpCode.Code == >> Code.Ret); >> >> .... >> >> foreach (Instruction instruction in exitInstructions) >> { >> processor.InsertBefore(returnInstruction, >> instruction); >> } >> >> However, sometimes this approach bombs and I get an Invalid Program >> exception, which seems to happen when there is a Br_S instruction >> before the Ret instruction....which I guess makes sense. >> >> So, my question is...how can I reliably determine where to insert my >> new instruction(s) at the end of the method, before it returns? >> >> Thanks in advance. > > -- > -- > mono-cecil -- -- mono-cecil
