Modify a method by adding boundary aspects (onEnter and onExit)

Topics: Metadata Model, Source Model
Jun 20, 2012 at 4:29 PM

Hi

I would like to modify some methods of my dll by adding boundary aspects to that i.e. add a method calls OnEnter and OnExit (coming from separate class - the aspect class).

I have following questions how to accomplish that:

1. As I've seen from some examples you need to implement you own MetadataWriter class and then the Rewrite method. However for this purpose do I need to also create specialized implementation for ILRewriter class?

2. How should I add a new local variable (the aspect object) to the existing method. Should I use IlGenerator.AddVariableToCurrentScope or should I manipulate the LocalVariables collection in the MethodBody class?

Thanks for any help with those questions.


Coordinator
Jun 20, 2012 at 5:29 PM

1) Yes, if you want to rewrite IL then it is best to subclass ILRewriter.

2) Let ILRewriter do the housekeeping for you. Just create a new local (new GeneratorLocal() { Method = ... } should do the trick) and stick into an instruction. ILRewriter will find it and get it into the LocalVariables collection. If you want it show up in the debugger, you'll have to do more work, but I assume that is not desirable in this case.

Jun 25, 2012 at 11:21 AM

Hi

I'm aware of a few projects based on cci that allow what you want with little effort, but currently my memory fails and I can provide only one link.

https://bitbucket.org/palpatine/wrist/wiki/Home 

If you wand exact code that rewrites method:

https://bitbucket.org/palpatine/wrist/src/2c555f9a7a04/Wrist.Savior/Wrist.Savior/MethodEnhancementsUtilizer.cs
its unpleasant code to read but it gets the job done.

Jun 25, 2012 at 6:15 PM

Thanks guys.

Rafal, I will definitely have a look at those projects,