gpt4 book ai didi

c# - 为什么调用 WindsorContainer 的 AddComponent 时要使用 key 参数?

转载 作者:行者123 更新时间:2023-11-30 15:13:04 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com