Interception Methods with Parameters

Jul 9, 2011 at 11:42 PM
Edited Jul 9, 2011 at 11:45 PM

I did not see an example of interception a method with parameters? 

 

 
// Note: line indented to improve readability
var foo = new Proxy<Foo>()
   .Target( new Foo() )
   .InterceptMethod ( f => f.Go(parameter?) )
   .OnBefore(()=> Console.Write(�Hello World!�) )
   .Save();

// Result: every time Go() is called a �Hello World!� message is previously printed.
foo.Go(); 


I think I found the Answer ..... 

var foo = new Proxy<Foo>()
   .Target( new Foo() )
   .InterceptMethod ( f,p => f.Go(p) )
   .OnBefore(()=> Console.Write(�Hello World!�) )
   .Save();

Coordinator
Jul 12, 2011 at 7:03 AM

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptMethod(f => f.GoWithParam( default(string) ))
	.OnBefore(()=> Console.Write("before..."))
	.Save();

foo.GoWithParam("some text");

If you have overloaded methods you can use:

.InterceptMethod(f => f.GoWithParam( It.Any<string>() ))

I hope it helps!