Dependency injection 温莎城堡可';t注入接口类型数组
我有一个类,它在构造函数中接受一系列接口:Dependency injection 温莎城堡可';t注入接口类型数组,dependency-injection,castle-windsor,Dependency Injection,Castle Windsor,我有一个类,它在构造函数中接受一系列接口: public class Foo<T1, T2> : IFoo<T1, T2> { public Foo(IBar[] bars) { ... } } 公共类Foo:IFoo { 公共食品(IBar[]条) { ... } } 我的集装箱登记如下: container.Register(AllTypes.Pick().FromAssemblyNamed("...")
public class Foo<T1, T2> : IFoo<T1, T2>
{
public Foo(IBar[] bars)
{
...
}
}
公共类Foo:IFoo
{
公共食品(IBar[]条)
{
...
}
}
我的集装箱登记如下:
container.Register(AllTypes.Pick().FromAssemblyNamed("...")
.WithService.FirstInterface());
container.AddComponent("foo", typeof(IFoo<,>), typeof(Foo<,>));
container.Register(AllTypes.Pick().FromAssemblyNamed(“…”)
.WithService.FirstInterface());
AddComponent(“foo”,typeof(IFoo),typeof(foo));
我有几个IBar的实现,容器可以明确地定位它们,因为调用ServiceLocator.Current.GetAllInstances()
效果很好
但是,如果我尝试获取IFoo的一个实例,它会抛出一个异常,表示它无法满足DeoEndence。。。“未注册”。
如果我将构造函数更改为使用IBar的单个实例,它就可以正常工作
有什么想法吗?添加阵列解决方案:
container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));
@Ben:目前还没有,但您可以在uservoice上建议::“通过xml配置添加子解析器”,或者更好的方法是分叉源代码并实现它;-)谢谢,这也帮我解决了。互联网上似乎到处都是人在改造这个类的自定义版本……如果我没有向服务注册,那么ArrayResolver没有解析这个数组,但使用服务修复了这个问题。