从类中调用时,城堡动态代理不拦截方法调用 [英] Castle Dynamic Proxy not intercepting method calls when invoked from within the class

查看:319
本文介绍了从类中调用时,城堡动态代理不拦截方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用城堡的动态代理时碰上了一点(我认为是)奇怪的行为。

I have run into a bit of (what I think is) strange behaviour when using Castle's Dynamic Proxy.

通过下面的代码:

class Program
{
    static void Main(string[] args)
    {
        var c = new InterceptedClass();
        var i = new Interceptor();

        var cp = new ProxyGenerator().CreateClassProxyWithTarget(c, i);

        cp.Method1();
        cp.Method2();

        Console.ReadLine();
    }
}

public class Interceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine(string.Format("Intercepted call to: " + invocation.Method.Name));

        invocation.Proceed();
    }
}

public class InterceptedClass
{
    public virtual void Method1()
    {
        Console.WriteLine("Called Method 1");
        Method2();
    }

    public virtual void Method2()
    {
        Console.WriteLine("Called Method 2");
    }
}



我期待得到的输出:

I was expecting to get the output:


  • 截获调用:方法

  • 调用的方法1

  • 截获来电:方法2

  • 调用方法2

  • 截获调用:方法2

  • 调用方法2

  • Intercepted call to: Method1
  • Called Method 1
  • Intercepted call to: Method2
  • Called Method 2
  • Intercepted call to: Method2
  • Called Method 2

但是我得到的是:


  • 截获来电:方法

  • 调用的方法1

  • 调用方法2

  • 截获调用:方法2

  • 调用方法2

  • Intercepted call to: Method1
  • Called Method 1
  • Called Method 2
  • Intercepted call to: Method2
  • Called Method 2

据我可以告诉那么动态代理只能够代理如果呼叫来自外部的程序调用,但没有从内部InterceptedClass上课的时候​​本身作为方法2被截获方法调用。

As far as I can tell then the dynamic proxy is only able to proxy method calls if the call comes from outside the class itself as Method2 was intercepted when called from Program but not from within InterceptedClass.

我可以种明白,从代理的类内拨打电话时,将不再通过代理,只是想检查,这是预期的,如果它然后看看有没有无论如何要得到所有来电,无论他们是从哪里叫截获?

I can kind of understand that when making calls from within the proxied class it would no longer go through the proxy, but just wanted to check that this was expected and if it is then see if there is there anyway to get all calls intercepted regardless of where they're called from?

感谢

推荐答案

编辑:TL;博士 - 我只是试着以不同的方式创建代理,如下所述,它产生你后的输出。我不得不改变这一点:

tl;dr - I've just tried creating the proxy in a different way, as described below, and it produces the output you were after. I just had to change this:

var c = new InterceptedClass();
var i = new Interceptor();

var cp = new ProxyGenerator().CreateClassProxyWithTarget(c, i);

要这样:

var i = new Interceptor();
var cp = new ProxyGenerator().CreateClassProxy<InterceptedClass>(i);






据我了解,代理生成是有效的创建一个包装对象。他们是两个不同的对象 - 一个是就在其他的包装,具有拦截等在包装层


As I understand it, the proxy generator is effectively creating a wrapper object. They're two separate objects - one is just a wrapper around the other, with interception etc in the wrapper layer.

这是很难看到它的可能的改变是什么 InterceptedClass 确实有自己的方法调用:

It's hard to see how it could change what the instance of InterceptedClass did with its own method calls:


  • DynamicProxy不能更改现有对象的类型;一旦创建一个对象,它的类型是固定

  • DynamicProxy无法更改来电如何利用现有的现有对象绑定

如果你想方法1 调用方法2 通过的了包装器使用当前的代理创建代码,你需要告诉现有对象有关的包装,无论是作为在其中一个字段或方法参数。

If you want Method1 to call Method2 via the wrapper using the current proxy creation code, you'll need to tell the existing object about the wrapper, either as a field within it or as a method parameter.

另外,在某种意义上之一,代理的的目标对象 - 有可能是创建代理开始与一个不同的方式。我怀疑你可能想看看 CreateClassProxy ,而不是 CreateClassProxyWithTarget - 我怀疑这是一个事实,即你的提供的,这是造成你的问题的目标对象。

Alternatively, there may be a different way of creating the proxy to start with - one where the proxy is in some sense the target object. I suspect you may want to look at CreateClassProxy rather than CreateClassProxyWithTarget - I suspect it's the fact that you're supplying the target object which is causing you problems.

无论你看到的行为是预期与否就取决于你的期望 - 但这肯定是的 I 的期望,而无需了解城堡动态代理什么:)

Whether the behaviour you're seeing is "expected" or not obviously depends on your expectations - but it's certainly what I would expect, without knowing anything about Castle Dynamic Proxy :)

这篇关于从类中调用时,城堡动态代理不拦截方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆