Похоже, мое предположение было верным.
Я попробовал тот же пример, только на этот раз создаю прокси прямо из класса:
Main(){
//proxy-ing an explicit type
A proxy = (A) new Castle.DynamicProxy.ProxyGenerator()
.CreateClassProxy<A>(new Interceptor());
proxy.foo();
}
Результатом было то, что я ожидал в первую очередь:
Intercepted foo
foo
Intercepted bar
bar
Это приводит меня к следующему выводу:
- при создании прокси из интерфейса он использует состав для делегирования вызовов реализации
- при создании прокси из типа (класса) он наследует от типа, поэтому все виртуальные вызовы в типе класса будут вызывать переопределенные методы в прокси.
При создании прокси интерфейса с реализацией интерфейса сгенерированный прокси выглядит примерно так:
class InterfaceProxy: IA { //implements interface
IA m_impl;
[...]
Proxy(IA i_impl){
m_impl = i_impl;
}
public void foo(){
//overly-simplified, but you get the picture
InvokeInterceptors("foo");
//execution gets here when calling 'invocation.Proceed()'
//from the interceptor
m_impl.foo(); //pass the execution to the implementation;
//the proxy has no more control over what gets executed.
}
public void bar(){
InvokeInterceptors("bar");
m_impl.bar();
}
}
При создании прокси класса код выглядит так:
class ClassProxy: A { //inherits class type
Proxy(): base() { ... }
public override void foo(){
InvokeInterceptors("foo");
//execution gets here when calling 'invocation.Proceed()'
//from the interceptor
base.foo(); //pass the execution to the base class
}
public void bar(){
InvokeInterceptors("bar");
base.bar();
}
}