【发布时间】:2010-06-14 17:14:02
【问题描述】:
我正在尝试将 Windsor 用作工厂,以提供基于 XAbstractBase 子类型(在我的例子中是抽象消息基类)的规范实现。
我有如下代码:
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*/ }
}
我的组件注册码是这样的:
container.Register(
AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn(typeof(ISpecification<>))
.WithService.FirstInterface()
)
当我尝试解析 ISpecification<YImplementation> 时,这很好用;它正确解析SpecificSpecificationImplementation。
但是,当我尝试解析ISpecification<ZImplementation>Windsor 时会抛出异常:
"No component for supporting the service ISpecification'1[ZImplementation, AssemblyInfo...] was found"
如果没有注册更多具体实现,Windsor 是否支持将通用实现解析为基类?
【问题讨论】:
标签: c# generics castle-windsor