城堡温莎不能注入接口类型的数组(Castle Windsor can't inject an

2019-06-24 09:36发布

我有一个类,它的构造函数接口数组:

public class Foo<T1, T2> : IFoo<T1, T2>
{
    public Foo(IBar[] bars)
    {
        ...
    }
}

我的容器注册如下所示:

container.Register(AllTypes.Pick().FromAssemblyNamed("...")
                    .WithService.FirstInterface());
container.AddComponent("foo", typeof(IFoo<,>), typeof(Foo<,>));

我有IBAR的几个实施方式中,并且容器可以肯定找到它们,因为主叫ServiceLocator.Current.GetAllInstances<IBar>()工作正常。

但是,如果我试图让IFoo的实例,它抛出一个异常说它不能满足deoendency ......“这是未注册”。

如果我改变构造采取伊巴尔的单个实例,它工作正常。

有任何想法吗?

Answer 1:

添加ArrayResolver:

container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel)); 


文章来源: Castle Windsor can't inject an array of interface types