温莎城堡 - 解决一个通用的实施,基本类型 [英] Castle Windsor - Resolving a generic implementation to a base type
问题描述
我试图用温莎作为一家工厂根据 XAbstractBase
的亚型(在我的情况下,一个抽象的消息的基类)提供规范的实现。
I'm trying to use Windsor as a factory to provide specification implementations based on subtypes of XAbstractBase
(an abstract message base class in my case).
我有类似下面的代码:
public abstract class XAbstractBase { }
public class YImplementation : XAbstractBase { }
public class ZImplementation : XAbstractBase { }
public interface ISpecification<T> where T : XAbstractBase
{
bool PredicateLogic();
}
public class DefaultSpecificationImplementation : ISpecification<XAbstractBase>
{
public bool PredicateLogic() { return true; }
}
public class SpecificSpecificationImplementation : ISpecification<YImplementation>
{
public bool PredicateLogic() { /*do real work*/ }
}
我的组件注册代码如下所示:
My component registration code looks like this:
container.Register(
AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn(typeof(ISpecification<>))
.WithService.FirstInterface()
)
这时候我尝试解决正常工作 ISpecification< YImplementation>
; 。它正确解析 SpecificSpecificationImplementation
This works fine when I try to resolve ISpecification<YImplementation>
; it correctly resolves SpecificSpecificationImplementation
.
然而,当我尝试解决 ISpecification< ZImplementation>
温莎抛出异常:
However, when I try to resolve ISpecification<ZImplementation>
Windsor throws an exception:
"No component for supporting the service ISpecification'1[ZImplementation, AssemblyInfo...] was found"
温莎是否支持解决通用实现到基类,如果没有更具体实施注册?
Does Windsor support resolving generic implementations down to base classes if no more specific implementation is registered?
推荐答案
请参阅的这个帖子。
好吧,我现在看到你在做什么错。您有没有服务 ISpecification< ZImplementation方式>
,因此它并不奇怪,温莎无法解决它。
Ok, I see now what you're doing wrong. You have no service for ISpecification<ZImplementation>
, hence it's not surprising that Windsor can't resolve it.
这不是一个问题,温莎在所有。
It's not a Windsor issue at all.
这篇关于温莎城堡 - 解决一个通用的实施,基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!