C# 在Castle Windsor中,如何为所有找到的泛型类型实现注册泛型接口的多个实现之一?

C# 在Castle Windsor中,如何为所有找到的泛型类型实现注册泛型接口的多个实现之一?,c#,generics,dependency-injection,inversion-of-control,castle-windsor,C#,Generics,Dependency Injection,Inversion Of Control,Castle Windsor,有接口 IThing IWrapping<IThing> 。。。和wrapping为他们准备 Paper <TThing> : IWrapping<TThing> where TThing is IThing Foil <TThing> : IWrapping<TThing> where TThing is IThing 如何一次注册所有这些文件 Component.For<IWrapping<IThing>&g

有接口

IThing
IWrapping<IThing>
。。。和
wrapping
为他们准备

Paper <TThing> : IWrapping<TThing> where TThing is IThing
Foil  <TThing> : IWrapping<TThing> where TThing is IThing
如何一次注册所有这些文件

Component.For<IWrapping<IThing>>()
    .ImplementedBy<Paper<ALL_FOUND_IMPLEMENTATIONS_OF_ITHING>>(), // One place to switch between Paper and Foil
Component.For()
.ImplementedBy(),//在纸张和箔纸之间切换的一个位置

因为您在Castle中处理泛型类型参数,所以不能像使用它一样使用fluent语法

您可以执行以下一行操作:

container.Register(Component.For(typeof(IWrapping<>)).ImplementedBy(typeof(Paper<>)));
var cookieWrapper = container.Resolve<IWrapping<Cookie>>();
container.Register(Component.For(typeof(IWrapping))。由(typeof(Paper))实现;
var cookieWrapper=container.Resolve();
解析依赖项后,您将得到以下结果:

这是基于以下对象依赖关系设置的(我镜像了您在帖子中的内容,但只是想确保您完全了解我为复制此内容所做的工作):

公共接口在{}
公共接口IWrapping{}
公开课论文:IWrapping where TThing:IThing{}
公共类Cookie:IThing{}
公共类卡梅尔:伊辛{}
公共类巧克力:我喜欢{}
Component.For<IWrapping<IThing>>()
    .ImplementedBy<Paper<ALL_FOUND_IMPLEMENTATIONS_OF_ITHING>>(), // One place to switch between Paper and Foil
container.Register(Component.For(typeof(IWrapping<>)).ImplementedBy(typeof(Paper<>)));
var cookieWrapper = container.Resolve<IWrapping<Cookie>>();
public interface IThing {}
public interface IWrapping<IThing> {}
public class Paper<TThing> : IWrapping<TThing> where TThing : IThing {}
public class Cookie : IThing {}
public class Carmel : IThing{}
public class Chocolate : IThing{}