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

标签 c# castle-windsor

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/

相关文章:

c# - 添加内联后 TextBlock 文本属性仍然为 null

c# - 无法从 .NET 3.5 连接到 SQL Server 2008

javascript - 如何从引导模式获取值并将其发送到 Controller C# MVC

.net - 我应该使用哪种依赖注入(inject)工具?

c# - 用于 log4net 的 CaSTLe 日志记录工具,具有流畅的 log4net 配置

c# - 谁能给我一个使用 BouncyCaSTLe 将 .pem 公共(public) DSA key 导入 c# 的示例?

c# - 将自定义创建的位图绘制到屏幕

c# - CaSTLe Windsor 在组件已注册后使用选择器应用拦截器

c# - 使用 AutoMapper.Profile 创建实例(非静态)映射器

caSTLe-windsor - CaSTLe Windsor CollectionResolver : Why doesn't it work on Resolve calls?