This project is read-only.

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

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


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.

Jun 20, 2012 at 6: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 12:21 PM


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. 

If you wand exact code that rewrites method:
its unpleasant code to read but it gets the job done.

Jun 25, 2012 at 7:15 PM

Thanks guys.

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