14

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).

Community
  • 1
  • 1
Markus Hi
  • 1,709
  • 18
  • 19

5 Answers5

8

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

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 12
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Daniel A. White Mar 17 '15 at 13:23
4

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...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I wonder if frameworks like these are all based off of reflection. Also, the PostSharp.org link here has changed to https://www.postsharp.net/ – C. Tewalt Apr 11 '16 at 03:08
  • @matrixugly: Fixed the link, but no, you can't do all of this just with reflection - you need IL rewriting, basically. – Jon Skeet Apr 11 '16 at 05:47
  • that's what I gathered. I not fond of the thought of a 3rd Party (or even my own) library mucking in the emitted code. Sounds like PostSharp actually scans the IL. I wonder if a simple version of this could load an assembly and use reflection to emit the "AOP-ized" version of the dll. (That way there's no scanning of the IL?) – C. Tewalt Apr 11 '16 at 12:27
  • @matrixugly: Well it would still need to look through the IL in order to emit the AOP-ized IL... I'm not quite sure what the objection is, to be honest... – Jon Skeet Apr 11 '16 at 12:37
  • Wouldn't it be possible to load the assembly and modify expression trees for existing methods, then output a new dll? That way, you're not explicitly looking through the IL, you're letting Reflection do the work... right? – C. Tewalt Apr 11 '16 at 15:40
  • @matrixugly: Well *something's* looking at the IL - and which library are you using to convert IL into expression trees and back? Sooner or later, there's code involved which understands IL, and allows you to manipulate it programmatically. There's System.Reflection.Emit, but that doesn't allow you to consume existing code and modify it in a pleasant way, as far as I know. – Jon Skeet Apr 11 '16 at 15:42
  • Makes sense. So then the only other thing I can think of would be to have something change the C# before compilation. Could probably use Roslyn to parse/modify the syntax trees in memory. But now we're down the road of swapping the compiler out altogether, unless Roslyn supports using plugins for custom code-gen. Also not sure how Visual Studio would play with the debugging symbols either. – C. Tewalt Apr 11 '16 at 16:16
2

Using only the standard .NET framework library, you would have to create the wrap functionality by deriving from System.Runtime.Remoting.Proxies.RealProxy.

This functionality you then can apply to your class, but this class has to derive from System.MarshalByRefObject.

This is quite some restriction, that's why you might want to look at 3rd party components like PostSharp.

herzmeister
  • 11,101
  • 2
  • 41
  • 51
1

You can do this without having to use a different compiler if you use PostSharp.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
1

I wrote some stuff about AOP on .Net here: Help and Information about Aspect Oriented Programming

Community
  • 1
  • 1
Henrik
  • 9,714
  • 5
  • 53
  • 87