C# 为什么赢了';t DynamicProxy';是否为*每个*虚拟方法调用调用s侦听器?
一个例子最能说明这一点:C# 为什么赢了';t DynamicProxy';是否为*每个*虚拟方法调用调用s侦听器?,c#,proxy-classes,castle-dynamicproxy,dynamic-proxy,C#,Proxy Classes,Castle Dynamicproxy,Dynamic Proxy,一个例子最能说明这一点: 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("bar"
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("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(a, new Interceptor());
proxy.foo();
}
我本以为会有这样的结果:
Intercepted foo
foo
Intercepted bar
bar
相反,我得到:
Intercepted foo
foo
bar
为什么?
动态代理是如何工作的?
我希望生成的代理继承自代理类,但是,它似乎使用组合将代理接口中的每个方法委托给实际实现
我尝试过Castle DynamicProxy,也尝试过旧的动态代理实现,从开始,您使用的方法是
CreateInterfaceProxy WithTarget
,它指示代理生成器为接口创建代理并将调用转发到目标对象,因此您看到的是您要求它执行的操作
如果希望代理从类派生,则需要使用
CreateClassProxy
方法
我尝试了相同的示例,只是这次直接从类类型创建代理:
Main(){
//proxy-ing an explicit type
A proxy = (A) new Castle.DynamicProxy.ProxyGenerator()
.CreateClassProxy<A>(new Interceptor());
proxy.foo();
}
这使我得出以下结论:
- 从接口创建代理时,它使用组合将调用委托给实现
- 从(类)类型创建代理时,它从该类型继承,因此类类型中的所有虚拟调用都将调用代理中被重写的方法
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();
}
}
创建类代理时,代码如下所示:
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();
}
}
当我看到你的答案时,我刚写完自己的答案:)看来我的推理是正确的。哇,你是动态城堡中的摇滚明星:)谢谢你写的教程!(或者应该说是教程;)