问题描述
我正在使用所附图片解释我的意思.
I'm using the attached image to explain what I meant.
-
我有一些由NInject管理的类.其中一些具有一些单例实例,而另一些处于瞬态范围内.在图像中,蓝色矩形为singltons,红色为瞬态.处理器依赖于其他类或实例.
I have a few classes managed by NInject. Some of them have a few singleton instances, and others are in transient scope. In the image, blue rectangles are singltons, red are transient. The Processor depends on other classes or instances.
我想每次通过使用kernel.Get获取Processor的实例.但是,每次我想对处理器使用的对象使用不同的值.请参阅图像中的Action1和Action2.该代码不是真实的,仅用于解释.
I want to get the instance of Processor each time by using kernel.Get. However, each time I want to use different values for the objects used by the Processor. See Action1 and Action2 in the image. The code is not real but just for explanation here.
是否有任何现有方法可以满足我的需求?
Is there any existing way can meet my needs?
推荐答案
您应该能够传递构造函数参数,前提是您的处理器将那些依赖项作为构造函数中的参数.
You should be able to pass constructor arguments given that your Processor takes those dependencies as arguments in the constructor.
var foo = new Ninject.Parameters.ConstructorArgument("foo", new Foo());
var bar = new Ninject.Parameters.ConstructorArgument("bar", new Bar());
var processor = kernel.Get<IProcessor>(foo, bar);
public Processor (Foo foo, Bar bar){
this.foo = foo;
this.bar = bar;
}
这篇关于NInject:如何在Get< T>()时传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!