将参数传递给深层类激活
我有以下代码:
class Controller
{
public Controller(Listener audioListener,
Listener videoListener)
{}
}
class Listener
{
public Listener(int port)
{
Console.WriteLine(port);
}
}
现在我需要使用 audioPort
解析 Listener
一次,使用 videoPort
解析另一次。
var audioPort = 1330;
var videoPort = 1331;
var controller = kernel.Get<Controller>(); // should print 1330 and 1331
到目前为止,我已经在与需要回调的 WithConstructorArgument
绑定时完成了此操作。我想知道在实际解析类型时是否可以设置回调?
I have the following code:
class Controller
{
public Controller(Listener audioListener,
Listener videoListener)
{}
}
class Listener
{
public Listener(int port)
{
Console.WriteLine(port);
}
}
Now I need that the Listener
is resolved one time with audioPort
and the other with videoPort
.
var audioPort = 1330;
var videoPort = 1331;
var controller = kernel.Get<Controller>(); // should print 1330 and 1331
So far I've done this when binding with WithConstructorArgument
that takes a callback. I was wondering if I can set the callback when actually resolving the type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做了一些研究,显然问题是不同的。
参数是运行时依赖项。我想要执行此操作的方式与服务定位器模式有关,该模式在测试中很复杂。
此处解释了使用运行时参数的更好方法 。
I did a little research, and obviously the problem is different.
The arguments are run-time dependencies. And the way I wanted to do this, is related to the service locator pattern, which is complicated in testing.
A better way of using run-time arguments is explained here.