注入类的构造函数参数

托马斯·魏斯

假设我想注入此接口的实现:

interface IService { ... }

实施为:

class MyService : IService
{
    public MyService(string s) { }
}

在此类的实例中:

class Target
{
    [Inject]
    public IService { private get; set; }
}

我通过调用进行注入kernel.Inject(new Target()),但是如果调用时我想s根据某些上下文指定构造函数的参数Inject怎么办?有没有一种方法可以在注入时实现此类依赖于上下文的服务初始化?

谢谢!

托马斯·魏斯

只需使用参数即可完成...

kernel.Inject(new Target(), new ConstructorArgument("s", "someString", true));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章