拦截不是从 ninject 内核创建的对象

发布于 2024-11-10 10:52:27 字数 712 浏览 8 评论 0原文

假设我有一个类:

public class SomeClass {
    public virtual void InterceptedMethod ()
    {
        // Do something.
    }
}

我正在使用带有拦截扩展的Ninject。我想拦截InterceptedMethod。如果我从 ninject 内核创建对象,该方法就会被拦截。

kernel = InitializeKernel ();
SomeClass objectFromKernel = kernel.Get<SomeClass> ();
objectFromKernel.InterceptedMethod (); // this method is intercepted.

但是,如果我不是从内核创建对象,则该方法不会被拦截。

kernel = InitializeKernel ();
SomeClass objectSelfCreated = new SomeClass ();
objectSelfCreated.InterceptedMethod (); // this method is NOT intercepted.

是否可以使用 ninject 拦截自创建的对象?如果可能的话,我该怎么做?

Let say I have a class :

public class SomeClass {
    public virtual void InterceptedMethod ()
    {
        // Do something.
    }
}

I'm using Ninject with interception extension. I want to intercept InterceptedMethod. If I created object from ninject kernel, that method is intercepted.

kernel = InitializeKernel ();
SomeClass objectFromKernel = kernel.Get<SomeClass> ();
objectFromKernel.InterceptedMethod (); // this method is intercepted.

But, if I created object not from kernel, that method is NOT intercepted.

kernel = InitializeKernel ();
SomeClass objectSelfCreated = new SomeClass ();
objectSelfCreated.InterceptedMethod (); // this method is NOT intercepted.

Is it possible to intercept self-created object using ninject ? If possible, how can I do that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒的傷心 2024-11-17 10:52:27

在这种情况下,任何 Ninject 都无法帮助你。您必须自己创建代理或更改设计,以便由 Ninject 创建对象。有关如何执行此操作的信息,请参阅代理工厂的文档。

No Ninject can not help you in this situation. You have to create a proxy yourself or change the design so that the object is created by Ninject. See the documentation of your proxy factory about how to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文