IWindsorContainer 接口上的 AddComponent 方法有几个重载,例如:
WindsorContainer.AddComponent<I,T>()
和
WindsorContainer.AddComponent<I,T>(string key)
关键参数有什么用,为什么要使用它?
IWindsorContainer 接口上的 AddComponent 方法有几个重载,例如:
WindsorContainer.AddComponent<I,T>()
和
WindsorContainer.AddComponent<I,T>(string key)
关键参数有什么用,为什么要使用它?
如果您注册了同一接口的多个实现,您将使用 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”键注册的。所以它实际上也可以在以后使用自动密钥检索。
希望这可以帮助。
终于在文档的某个地方找到了它。
他们说在这个页面的一半左右
请注意,可以为同一服务添加多个实现。在这种情况下,当您通过服务请求组件时,将返回为该服务注册的第一个组件。要获得同一服务的其他实现,您必须使用密钥。