[英]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.