IWindsorContainer 接口(interface)上的 AddComponent 方法有几个重载,例如:

WindsorContainer.AddComponent<I,T>()


WindsorContainer.AddComponent<I,T>(string key)

key参数有什么用,我为什么要使用它?

最佳答案

如果您注册了同一接口(interface)的多个实现,您将使用 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");

另外,当您注册一个没有 key 的组件时,我相信它的类型被用作 key 。
container.AddComponent<IHandler, FileHandler>();

我相信这是用“{Namespace}.IHandler”的键注册的。因此,它实际上也可以在以后使用自动 key 进行检索。

希望这可以帮助。

关于c# - 为什么在 WindsorContainer 上调用 AddComponent 时要使用 key 参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/468681/

10-13 03:12