I have some mechanics to measure method execution time. Simply at start of method i save time and at end i save time and i compare these two values.
But every time i want to use that i need to alter method body.
I am seeking gor a cleaner way – like to have one object responsible for measuring time which could encapsulate method call – like some wrapper, call that method, pass returned value and in that process also measure time.
Also, i know that it might be a little sci-fi, but is there way to wrap or attach to all methods (or even subset of methods)? It could be used for example also for logging – like “i am calling method of instance XXX with paremeter YYY and returned value vas ZZZ”?
I know something like this was possible in Java EE. (I think it was called interceptors if i remember correctly and they could be attached to some or all methods and add functionality, but i dont know whether is this possible in .net)
Thank you.
A great way here is to use aspect oriented programming, like PostSharp…and here’s the example to use it for time measuring: http://theburningmonk.com/2010/03/aop-method-execution-time-watcher-with-postsharp/
The advantage is, that you just add an attribute to your method and don’t have to alter / “contaminate” the method body.
Cheers,
Christian