Via Windsor I将多个实现类型注册到单个接口类型:

public class WindsorInstaller : IWindsorInstaller
  {
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
      container.Register(
        Component.For<IMyType>()
          .ImplementedBy<MyTypeClass1>()
          .Named("MyTypeClass1")
          .LifestyleTransient());

      container.Register(
        Component.For<IMyType>()
          .ImplementedBy<MyTypeClass2>()
          .Named("MyTypeClass2")
          .LifestyleTransient());
    }
  }
}

我有一节消费课:
public class MyConsumingClass : IMyConsumingClass
  {
    private readonly IList<IMyType> _myObjects;

    public MyConsumingClass(IEnumerable<IMyType> myObjects)
    {
      _myObjects = myObjects.ToList();
    }
}

但是在运行时,我收到以下异常:
Can't create component 'MyConsumingClass' as it has dependencies to be satisfied. 'MyConsumingClass' is waiting for the following dependencies: - 'System.Collections.Generic.IEnumerable`1[[IMyType, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' which was not registered.

最佳答案

我想您可能需要在容器中添加一个CollectionResolver
尝试:

kernel.Resolver.AddSubResolver(new CollectionResolver(kernel));

08-16 10:55