5

Is there a way to wrap methods in other methods transparently in C#? I want to achieve what is done by Moose's around functionality: http://search.cpan.org/perldoc?Moose::Manual::MethodModifiers

EDIT: And by transparent I mean without modifying the original method.

Joel Beckham
  • 18,254
  • 3
  • 35
  • 58
rpkelly
  • 2,076
  • 1
  • 20
  • 19

5 Answers5

4

I think you're looking for what's termed Aspect Oriented Programming. There are many C# libraries to help with this. One is called PostSharp (The free version of PostSharp supports this functionality). Here is an example similar to the moose example. This creates a Trace Attribute which you can use on other methods to tack on this extra functionality:

[Serializable]
public class TraceAttribute : OnMethodBoundaryAspect
{

    public override void OnEntry( MethodExecutionArgs args )
    {
        Trace.WriteLine("about to call method");
    }

    public override void OnExit(MethodExecutionArgs args) 
    { 
       Trace.WriteLine("just finished calling method"); 
    }
 }

You would add it to method "Foo" by placing the Trace attribute right before it:

[Trace]
public void Foo() { /* ... */ }

Now when Foo executes, the above OnEntry method will run before it, and OnExit will run right after.

Joel Beckham
  • 18,254
  • 3
  • 35
  • 58
  • can I modify the arguments as they are passed to the function? I see that I receive them, but I can't tell from the documentation whether I can alter them. – rpkelly Feb 02 '11 at 05:57
  • @rpkelly - What specifically are you hoping to do with the arguments? – Joel Beckham Feb 02 '11 at 16:29
  • Well I need to log calls to the function and its return values, as well as handle the automatic generation of a "transaction id" which is passed into the function (which is currently generated elsewhere). – rpkelly Feb 03 '11 at 14:05
2

Indeed, they're called "delegates" in .NET. See:

for help.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Alex Dresko
  • 5,179
  • 3
  • 37
  • 57
  • 2
    Close. But I need to do it without rewriting the original method. This is what I meant by transparent. Although, this is neat :) – rpkelly Feb 01 '11 at 21:40
2

You can achieve the same effect by utilizing a dynamic proxy. An example is the Castle Dynamic Proxy.

Such frameworks leverage the C# reflection facilities to construct 'proxy' or 'wrapper' classes. So, keep that in mind. There is a certain amount of overhead because of this. Alternatively you can use frameworks that can create classes statically via code generation.

Ziffusion
  • 8,779
  • 4
  • 29
  • 57
0

No, not the way it's done in Moose. You might want to look into some AOP library.

helium
  • 1,077
  • 1
  • 9
  • 15
0

Some isolation libraries implement functionality that allows replacing calls to methods with "detours" or mock methods. You may be able to use the same functionality to implement the interception you are referring to. For more details, check the following:

Rhino Mocks stubs and mocks are only good for interfaces?

http://research.microsoft.com/en-us/projects/moles/

http://www.typemock.com/typemock-isolator-product3/

Community
  • 1
  • 1
JeremyDWill
  • 3,132
  • 21
  • 18