Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap a method via attributes?

Tags:

c#

attributes

I'm wondering if it's possible to wrap a method only by adding an attribute.

Example: I want to log the execution time a method takes.

[LogTimings]
public void work()
{
  ..
}

This is kind of wrapping a method into another one (see this python implementation).

like image 866
Markus Hi Avatar asked Feb 05 '10 10:02

Markus Hi


2 Answers

AOP is possible in .NET. Here's an article about it. And here's a list of AOP frameworks for .NET.

like image 180
Darin Dimitrov Avatar answered Nov 12 '22 01:11

Darin Dimitrov


Have a look at PostSharp, an AOP framework for .NET.

In terms of logging and timing, there's also a framework called Gibraltar which integrates with PostSharp and which should make it easier to collect and use the results. I keep meaning to get round to trying it...

like image 4
Jon Skeet Avatar answered Nov 12 '22 01:11

Jon Skeet