简体   繁体   English

如何在给定接口祖先的情况下向Castle Windsor注册默认接口?

[英]How do I register default interfaces with Castle Windsor given an interface ancestor?

I have the following: 我有以下几点:

interface IAncestor { }

interface IDescendant1 : IAncestor { }

interface IDescendant2 : IAncestor { } 

class Descendant1 : IDescendant1 { }

class Descendant2 : IDescendant2 { }

What I would like to be able to do is automatically have Castle Windsor find all IDescendantX - DescendantX pairs without me having to specify them manually. 我想做的是自动让Castle Windsor查找所有IDescendantX - DescendantX对,而无需我手动指定它们。 Is this possible? 这可能吗?

I've tried: 我试过了:

        container.Register(
            Classes.FromThisAssembly()
            .BasedOn<IAncestor>()
            .WithService.DefaultInterfaces()
            .LifestyleTransient()
        );

but this does not find the default interfaces. 但这找不到默认接口。 (I'm having trouble phrasing my question with the right terminology, so could not find a topic on SO that already answers this, sorry if it's a duplicate...) (我在用正确的术语表述我的问题时遇到了麻烦,因此无法在SO上找到已经回答此问题的主题,抱歉,如果重复的话...)

Think the problem here is lack of access modifiers. 认为这里的问题是缺少访问修饰符。 If you add IncludeNonPublicTypes() the following test passes: 如果添加IncludeNonPublicTypes(),则以下测试通过:

[Test]
public void Test() 
{        
    //Arrange
    WindsorContainer sut = new WindsorContainer();

    //Act
    sut.Register(
            Classes.FromThisAssembly()
            .IncludeNonPublicTypes()
            .BasedOn<IAncestor>()
            .WithService.DefaultInterfaces()
            .LifestyleTransient());

    //Assert
    Assert.That(sut.Resolve<IDescendant1>(), Is.InstanceOf<Descendant1>());
    Assert.That(sut.Resolve<IDescendant2>(), Is.InstanceOf<Descendant2>());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM