为什么*每个*虚拟方法调用不会调用 DynamicProxy 的拦截器?
一个例子最好地解释了这一点:
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 以及旧的动态代理实现,来自 Cramon
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("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();
}
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来我的猜测是对的。
我尝试了相同的示例,只是这次直接从类类型创建代理:
结果正是我最初所期望的:
这使我得出以下结论:
当使用接口实现创建接口代理时,生成的代理看起来像这样:
当创建类代理时,代码看起来像这样:
Looks like my guess was right.
I tried the same example, only this time creating the proxy directly from the class type:
The result was what I expected in the first place:
This leads me to the following conclusion:
When creating an interface proxy with an interface implementation, the generated proxy looks something like this:
When creating a class proxy, the code looks like this:
您使用的方法
CreateInterfaceProxyWithTarget
指示代理构建器为接口创建代理并将调用转发到目标对象,因此您所看到的就是您要求它执行的操作。如果您希望代理从您的类派生,那么您需要使用 CreateClassProxy 方法。
You're using the method
CreateInterfaceProxyWithTarget
which instructs the proxy builder to create a proxy for the interface and forward the calls to the target object, so what you're seeing is what you've asked it to do.If you want the proxy to derive from your class then you'd need to use the
CreateClassProxy
method instead.