C# Unity容器Ctor注入
假设我们已经:
class A
{
ILogger myLog;
A(ILogger log)
{
this.myLog = log;
}
...
}
并且我们之前已经在unity容器中注册了ILogger接口,例如
container.RegisterType<ILogger, SomeLogger>();
这里是SomeLogger类:
class SomeLogger : ILogger
{
string myString;
SomeLogger(string test)
{
myString = test;
}
...
}
现在,unity如何为类A创建SomeLogger的实例而不将字符串传递给SomeLogger的ctor? 假设 SomeLogger 没有其他 ctor。在哪里可以指定映射 SomeLogger 类型的 ctor 的参数?
Say we have:
class A
{
ILogger myLog;
A(ILogger log)
{
this.myLog = log;
}
...
}
And we have registered the ILogger interface before in the unity container, e.g.
container.RegisterType<ILogger, SomeLogger>();
And here the SomeLogger class:
class SomeLogger : ILogger
{
string myString;
SomeLogger(string test)
{
myString = test;
}
...
}
Now, how can unity create an instance of SomeLogger for class A without passing a string to the ctor of SomeLogger?
Suppose there is no other ctor for SomeLogger. Where can one specify the parameter(s) for ctor of the mapped SomeLogger type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在配置中执行此操作:
这也以声明方式注册您的类型,因此
不再需要调用。
-道格
You can do that in your configuration:
This also declaratively registers your type, so the
call is no longer necessary.
-Doug
你可以这样做:
You can do this:
您也可以在注册代码中执行此操作,如下所示:
you could also do this in the registration code as follows: