如何通过Castle Windsor接口注册开放泛型类型?

时间:2011-11-09 08:14:24

标签: c# castle-windsor

我正在尝试进行此注册工作。我有一些IEventFactory<>负责在我的应用程序中创建不同的(外部)事件。

我想通过它的界面解决所有类型的工厂,例如IEventFactory<ClassA>。如何让我的测试成功?

[TestFixture]
public class WindsorTests
{ 

    [Test]
    public void Test_factory_registration()
    {           

        WindsorContainer container = new WindsorContainer();
        container.AddFacility<TypedFactoryFacility>();

        container.Register(AllTypes.FromThisAssembly().BasedOn(typeof(IEventFactory<>)).WithService.AllInterfaces());

        IEventFactory<ClassA> factoryA = container.Resolve<IEventFactory<ClassA>>();
        IEventFactory<ClassB> factoryB = container.Resolve<IEventFactory<ClassB>>();

    }


    class ClassA
    {

    }

    class ClassB
    {

    }


    interface IEventFactory<TCaller>
    {
        void Foo();
    }


    class EventFactoryA : IEventFactory<ClassA>
    {

        public void Foo()
        {
            throw new NotImplementedException();
        }
    }

    class EventFactoryB : IEventFactory<ClassB>
    {

        public void Foo()
        {
            throw new NotImplementedException();
        }
    }

}

2 个答案:

答案 0 :(得分:2)

您的界面和工厂是私有的。要么将它们设为公开/内部,要么在注册行中添加IncludeNonPublicTypes()

答案 1 :(得分:1)

要添加@Patrick Steele所说的内容,您可能也应该使用.WithService.Base()

查看at the documentation详细说明差异。