ninject拦截器-辅助方法 [英] ninject interceptor - helper methods

查看:148
本文介绍了ninject拦截器-辅助方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的界面:

public interface ITest
{
      void Method1();
      void Method2();
}

和实现:

public class Test:ITest
{
      public void Method1()
      {

      }
      public void Method2()
      {
           //Method1();
      }
}

自定义拦截器:

public class CustomInterceptor:IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
          invocation.Proceed();
    }
}

现在,当我执行两种方法时:

Now, when I execute there two methods:

ITest obj = getting through ninject
obj.Method1();
obj.Method2();

我的拦截器呼唤两次就可以了. 但是,当我取消注释Method2()的主体时,则不会调用Method1()的拦截器.我正在寻找要执行的操作,因为我希望触发拦截器. 当我从第二个开始调用Method1时,我知道生成的代理没有调用此方法,这就是为什么它不起作用的原因.但是有可能以同样的方式做到吗?

my interceptor is calling twice what is ok. But when I uncomment the body of Method2(), then the interceptor for the Method1() is not called. I'm looking for what to do, because I want the interceptor to be fired. When I call the Method1 from the second, I understand this is not called by the generated proxy and that's why it doesn't work. But is it possible to do it in same way?

推荐答案

Ninject围绕Test类的实际实例创建一个代理对象.您的方法不是虚拟的,因此对代理的任何替代都应使用新"而不是替代"来创建.因此,如果您从Method2调用Method1,则不会进行虚拟查找来查找和调用代理.

Ninject creates a proxy object around the actual instance of the Test class. Your methods aren't virtual, so any override for the proxy should be created with 'new' rather than 'override'. Thus, if you call Method1 from Method2, there is no virtual lookup to find the proxy and invoke it.

这篇关于ninject拦截器-辅助方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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