IWindsorContainer接口上的AddComponent方法有几个重载,例如:
WindsorContainer.AddComponent<I,T>()
和
WindsorContainer.AddComponent<I,T>(string key)
关键参数的用途是什么?为什么要使用它?
答案 0 :(得分:4)
如果您注册了同一界面的多个实现,则可以使用key参数。这样你以后可以检索一个特定的。例如,我可能有多个版本的IHandler。
container.AddComponent<IHandler, FileHandler>("handlers.file");
container.AddComponent<IHandler, HttpHandler>("handlers.http");
//I can retrieve the first one like this (or something like this).
IHandler fileHandler = container.Resolve<IHandler>();
//I can retrieve the http handler like this
IHandler httpHandler = container.Resolve<IHandler>("handlers.http");
此外,当您注册没有密钥的组件时,我相信它的类型被用作密钥。
container.AddComponent<IHandler, FileHandler>();
我相信这是使用“{Namespace} .IHandler”键注册的。因此,以后也可以使用自动密钥检索它。
希望这有帮助。
答案 1 :(得分:1)
最后在文档中找到了它。
大约一半on this page他们说
请注意,可以添加同一服务的多个实现。在这种情况下,当您通过服务请求组件时,将返回为该服务注册的第一个组件。要获得相同服务的其他实现,您必须使用密钥。