在温莎城堡中使用泛型参数解析泛型
我正在尝试注册类似 IRequestHandler1[GenericTestRequest
1[T]] 的类型,该类型将由 GenericTestRequestHandler`1[T] 实现,但我目前从 Windsor 收到错误“Castle.MicroKernel.ComponentNotFoundException” :没有支持该服务的组件“是否支持这种类型的操作?或者它是否远离支持的寄存器( Component.For(typeof( IList<>).ImplementedBy( typeof( List<> ) ) )
下面是破坏测试的示例。 /////////////////////////////////////////////////////////// ////
public interface IRequestHandler{}
public interface IRequestHandler<TRequest> : IRequestHandler where TRequest : Request{}
public class GenericTestRequest<T> : Request{}
public class GenericTestRequestHandler<T> : RequestHandler<GenericTestRequest<T>>{}
[TestFixture]
public class ComponentRegistrationTests{
[Test]
public void DoNotAutoRegisterGenericRequestHandler(){
var IOC = new Castle.Windsor.WindsorContainer();
var type = typeof( IRequestHandler<> ).MakeGenericType( typeof( GenericTestRequest<> ) );
IOC.Register( Component.For( type ).ImplementedBy( typeof( GenericTestRequestHandler<> ) ) );
var requestHandler = IoC.Container.Resolve( typeof(IRequestHandler<GenericTestRequest<String>>));
Assert.IsInstanceOf <IRequestHandler<GenericTestRequest<String>>>( requestHandler );
Assert.IsNotNull( requestHandler );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这里的问题是服务类型不是泛型类型定义,而实现类型是。下面的测试全部通过,证明了这一点:
这是因为接口上实际开放的参数类型是“inner”类型,而不是“result”类型。
因此,这就像使用接口
ICollection
(不是通用ICollection
!)和实现类型List
注册组件。当您向 Windsor 请求ICollection
时,它不知道要应用于实现类型的类型参数。在您的情况下,情况更糟,因为您要求的
IRequestHandler>
并未真正注册。 (IRequestHandler>
是)希望这是清楚的...
I think the problem here is that the service type is not a generic type definition, while the implementation type is. The following tests all pass, which proves this:
This is because the actual open parameter type on the interface is the "inner" Type, and not the "resulting" type.
So this would be like registering a component with interface
ICollection
(not the genericICollection
!) and implementation typeList<>
. When you ask Windsor forICollection
, it doesn't know what type parameter to apply to the implementation type.In your case it's even worse because you're asking for
IRequestHandler<GenericTestRequest<String>>
which isn't really registered. (IRequestHandler<GenericTestRequest<>>
is)Hope that was clear...