tags:

views:

41

answers:

3

Hi,

I like to have event like OnMethodEntry and OnMethodExit which will be used to raise an event when a method in a class is getting invoked in C#.NET.

I have seen Post Sharp which give this feature. Since it is third party tool, we like to design a library something similar to that.

How can I do that?

Thanks, P.Gopalakrishnan.

A: 

One way to do this is to modify assemblies by inserting these method calls manually in the IL.

Jason
Can you please give some idea about how to do this?
gopal
An easy way to accomplish this is to disassemble an IL assembly using `ildasm`, parse the resulting file to find all method entry and exit points and insert invocations of the events `OnMethodEntry` and `OnMethodExit` and then recompile using `ilasm`. There are more sophisticated ways but you'll need to take a deep dive into `System.Reflection.Emit` to implement them.
Jason
Thank You for your reply. I will try this. Thank You again.
gopal
+1  A: 

CodeProject has a nice article that I think will meet your needs: MethodLogger - Hook into method calls in .NET binaries.

Jim Lamb
A: 

You may try Cecil or CCI. They are good and mature libraries for reading and patching assemblies.

elder_george