简体   繁体   English

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

[英]How to register open generic type by interface with Castle Windsor?

I'm trying to make this registration work. 我正在努力使这个注册工作。 I have a number of IEventFactory<> responsible for creating different (external) events within my application. 我有一些IEventFactory<>负责在我的应用程序中创建不同的(外部)事件。

I'd like to resolve all kinds of factories just by it's interface, eg IEventFactory<ClassA> . 我想通过它的接口来解析各种工厂,例如IEventFactory<ClassA> How do I make my test below succeed? 如何让我的测试成功?

[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();
        }
    }

}

Your interfaces and factories are private. 您的接口和工厂是私有的。 Either make them public/internal or add IncludeNonPublicTypes() in the registration line. 将它们设置为公开/内部,或在注册行中添加IncludeNonPublicTypes()

To add to what @Patrick Steele said, you probably also should be using .WithService.Base() 要添加@Patrick Steele所说的内容,您可能还应该使用.WithService.Base()

Have a look at the documentation which details the differences. 请查看详细说明差异的文档

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

相关问题 在Castle Windsor中,如何为所有找到的通用类型的实现注册通用接口的许多实现之一? - In Castle Windsor, how to register ONE of many implementations of generic interface for ALL found implementations of generic type? 在 Castle Windsor 中注册泛型接口的多个实现 - Register multiple implementations of generic interface in Castle Windsor Castle windsor解决了具有开放式通用和接口的阵列 - Castle windsor resolve array with open generic and interface Castle Windsor注册了一个界面 - Castle Windsor register an interface 使用Castle Windsor解析具有通用类型的接口 - Resolving interface with generic type using Castle Windsor 使用Castle Windsor解决具有泛型类型约束的接口 - Resolving Interface with generic type constraint with Castle Windsor 你如何使用 Castle Windsor - Fluent Interface 来注册一个通用接口? - How do you use Castle Windsor - Fluent Interface to register a generic interfaces? 城堡温莎公约注册-如何基于实体注册通用存储库? - Castle Windsor Register by Convention - how to register generic Repository based on Entity? Castle Windsor解决了一个通用接口 - Castle Windsor resolving a generic interface 如何使用Castle Windsor注册通用装饰器? - How can I register a generic decorator using Castle Windsor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM