An example explains it best :
public interface IA {
void foo();
void bar();
}
public class A : IA {
public virtual void foo(){
Console.Write("foo");
bar(); //call virtual method
}
public virtual void bar(){
Console.Write("foo");
bar();
}
}
public class Interceptor : IInterceptor {
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Intercepted: " + invocation.Method.Name);
invocation.Proceed();
}
}
Main(){
IA a = new A();
//proxy-ing an interface, given an implementation
IA proxy = new Castle.DynamicProxy.ProxyGenerator()
.CreateInterfaceProxyWithTarget(IA, new Interceptor());
proxy.foo();
}
I would have expected the output:
Intercepted foo
foo
Intercepted bar
bar
Instead, I get:
Intercepted foo
foo
bar
Why?
How does the dynamic proxy work? I was expecting the generated proxy to inherit from the proxied class, however, it seems that it uses composition to delegate each of the methods in the proxied interface to the actual implementation.
I've tried with Castle DynamicProxy and also with an older dynamic proxy implementation, from Cramon