C# Castle Windsor:如何注册多种类型以实现特定接口?

C# Castle Windsor:如何注册多种类型以实现特定接口?,c#,dependency-injection,castle-windsor,C#,Dependency Injection,Castle Windsor,已更新 我有以下类/接口: public interface IFoo { (...) } public interface IFoo<T> : IFoo { (...) } public abstract BaseFoo<T> : IFoo<T> { (...) } public Bar : BaseFoo<ConcreteType1> { (...) } public Baz : BaseFoo<C

已更新

我有以下类/接口:

public interface IFoo
{
    (...)
}

public interface IFoo<T> : IFoo
{
    (...)
}

public abstract BaseFoo<T> : IFoo<T>
{
    (...)
}

public Bar : BaseFoo<ConcreteType1>
{
    (...)
}

public Baz : BaseFoo<ConcreteType2>
{
    (...)
}
公共接口IFoo
{
(...)
}
公共接口IFoo:IFoo
{
(...)
}
公共摘要BaseFoo:IFoo
{
(...)
}
公共酒吧:BaseFoo
{
(...)
}
公共Baz:BaseFoo
{
(...)
}
使用Castle Windsor,如何将类型Bar和Baz注册为实现IFoo

我已经尝试过一些失败的方法,比如:

Register(Types.FromAssembly(typeof (IFoo).Assembly)
        .BasedOn<IFoo>()
        .WithService
        .Select(new List<Type> { typeof(IFoo)})
        .Configure(x => x.LifeStyle.Is(LifestyleType.Singleton)));
寄存器(Types.fromsassembly(typeof(IFoo).Assembly)
.BasedOn()
.带服务
.Select(新列表{typeof(IFoo)})
.Configure(x=>x.LifeStyle.Is(LifestyleType.Singleton));

EDIT:注释中声明的异常表明抽象类
BaseFoo
正在尝试解析。这是因为您使用
类型
,它选择所有组件,甚至是抽象类。避免这种情况的一个简单方法是使用没有这个问题的
类。各国:

我应该使用类还是类型

有两种方法可以开始 按公约登记。其中之一是使用静态类, 就像上面的例子一样。第二种是使用类型静态类。他们 两者都公开完全相同的方法。他们之间的区别是,, 这些类型将允许您注册所有(或者更准确地说,如果您 使用给定程序集的默认设置(所有公共)类型,即 类、接口、结构、委托和枚举。网上课程 其他手<强>预过滤类型只考虑非抽象 类。大多数时候,类是您将要使用的,但是类型可以 在某些高级场景中非常有用,例如注册 基于接口的类型化工厂


这段代码看起来是正确的-事实上,我用三个不同的
调用service
进行了尝试(和),每一个调用都成功了。我认为这可能是您选择的第一部分不正确:
Types.fromsassembly(typeof(IFoo.Assembly)

我没有找到任何方法来获取castle在尝试使用fluent注册时考虑的内容列表,但您可以使用
Configure
来记录考虑的组件的名称:

c.Register(Classes.FromAssemblyInThisApplication()
    .BasedOn<IFoo>()
    .Configure(x => Console.WriteLine(x.Implementation.Name)) // classes names
    .WithService.Select(new Type[] {typeof(IFoo)})
    .Configure(x => x.LifeStyle.Is(LifestyleType.Singleton)));
c.Register(Classes.fromsassemblyinthisapplication()
.BasedOn()
.Configure(x=>Console.WriteLine(x.Implementation.Name))//类名称
.WithService.Select(新类型[]{typeof(IFoo)})
.Configure(x=>x.LifeStyle.Is(LifestyleType.Singleton));
因此,请尝试检查您是否使用了正确的程序集,或者您的类是否对容器可见

编辑

此代码适用于:

public interface IFoo
{
}
public interface IFoo<T> : IFoo
{
}
public abstract class BaseFoo<T> : IFoo<T>
{
}
public class Bar : BaseFoo<Bar>
{
}
public class Baz : BaseFoo<Baz>
{
}
// main
var c = new Castle.Windsor.WindsorContainer();
c.Register(Classes.FromAssemblyInThisApplication()
            .BasedOn<IFoo>()
            .Configure(x => Console.WriteLine(x.Implementation.Name))
            .WithService.Select(new Type[] {typeof(IFoo)})
            .Configure(x => x.LifeStyle.Is(LifestyleType.Singleton)));
var all = c.ResolveAll<IFoo>(); // 2 components found
公共接口IFoo
{
}
公共接口IFoo:IFoo
{
}
公共抽象类BaseFoo:IFoo
{
}
公共类酒吧:BaseFoo
{
}
公共类Baz:BaseFoo
{
}
//主要
var c=newcastle.Windsor.WindsorContainer();
c、 寄存器(类。FromAssemblyInThisApplication()
.BasedOn()
.Configure(x=>Console.WriteLine(x.Implementation.Name))
.WithService.Select(新类型[]{typeof(IFoo)})
.Configure(x=>x.LifeStyle.Is(LifestyleType.Singleton));
var all=c.ResolveAll();//发现2个组件

我认为问题并不是那么简单,我更新了我的代码样本,使其与我的具体问题相匹配。我刚刚用所描述的结构更新了我的测试,并且效果也很好(请参见编辑后的答案)。尝试添加我在类选择中建议的
Configure
调用,以查看过滤器返回的内容。我收到以下消息:{“请求的类型[Namespace]。IFoo有0个泛型参数,而组件实现类型[Namespace]。BaseFoo`1[T]需要1。\r\n这意味着Windsor没有足够的信息为您正确创建该组件。\r\n您可以通过提供IGenericImplementMatchingStrategy的实现来指示Windsor应该使用哪种类型来关闭此通用组件。\r\n请参阅文档以获取如何执行此操作的示例。“}您收到的错误消息是因为您使用了
类型
而不是
类型
选择所有类型,包括抽象类<代码>类
没有。我认为你犯的错误是nohing正在被解决,而不是你收到了这个异常;请毫不犹豫地将其添加到您的question@zloidooraque这是一种明确列出注册过滤类的接口的方法