I do similar things in a project I'm working on. I believe you can make
everything debuggable again by updating the sequence points in the debugging
symbols. I think you can do this fairly easily with Cecil. Also, you might
take a look at the attributes the C# compiler applies to say, iterator
blocks or anonymous methods so the debugger treats your separate methods as
one.
On the other hand, if you just want to add some logic before the
instrumented method returns, you might also just put that logic in a
separate method and insert a call to that method before every Ret
instruction, something like:
foreach (var ret in method.Body.Instructions.Where (i => i.OpCode ==
OpCodes.Ret)) {
il.InsertBefore (ret, il.Create (OpCodes.Call,
methodWithLogicToDoBeforeReturning));
}
As long as methodWithLogicToDoBeforeReturning is void-returning and has no
args, everything should be cool with the stack.
HTH,
Alex
On Sun, Nov 21, 2010 at 2:00 PM, Simon Cropp <[email protected]> wrote:
> Jeff
>
> Ok I have a suggestion
>
> Lets say u are modifying MethodA .
>
> 1. Create a new MethodX
> 2. Move all the instructions of MethodA into MethodX (Method A is now
> empty)
> 3. In MethodA now place the following code
> Call MethodX and assign to local varibale
> Insert your custom instuctions
> Return the local variable
>
> The one problem with this is MethodX will now not be debuggable. Not
> sure if this is an issue for you.
>
>
--
--
mono-cecil