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

查看:37
本文介绍了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 类的实际实例的代理对象.您的方法不是虚拟的,因此应该使用new"而不是override"创建代理的任何覆盖.因此,如果您从 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天全站免登陆