温莎能够收集具体类的实例吗?

时间:2012-09-17 09:05:48

标签: castle-windsor windsor-3.0

如果我有接口和实现类

public interface IA {}
public class X : IA {}
public class Y : IA {}

然后这个注册很好

var w = new WindsorContainer();
w.Register(Component.For<IA>().ImplementedBy<X>());
w.Register(Component.For<IA>().ImplementedBy<Y>());

相同
var w = new WindsorContainer();
w.Register(Component.For<IA>().Instance(new X()));
w.Register(Component.For<IA>().Instance(new Y()));

但是如果我尝试将具体类注册为

var w = new WindsorContainer();
var x1 = new X();
var x2 = new X();
w.Register(Component.For<X>().Instance(x1));
w.Register(Component.For<X>().Instance(x2));

它抛出异常:Castle.MicroKernel.ComponentRegistrationException : Component X could not be registered. There is already a component with that name. Did you want to modify the existing component instead? If not, make sure you specify a unique name.

如果是意图限制 - 为什么?有没有办法在不添加并不总是必要的界面的情况下实现集合解析?

1 个答案:

答案 0 :(得分:1)

  

您是否要修改现有组件?如果没有,请制作   确定你指定了一个唯一的名称。

Component.For<X>().Instance(x1).Named("x1")