c# - CaSTLe Windsor - 将泛型实现解析为基类型

标签 c# generics castle-windsor

我正在尝试使用 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>温莎抛出异常:

"No component for supporting the service ISpecification'1[ZImplementation, AssemblyInfo...] was found"

如果没有注册更具体的实现,Windsor 是否支持将通用实现分解为基类?

最佳答案

参见 this发布。

更新

好的,我现在知道你做错了什么了。您没有服务ISpecification<ZImplementation> ,因此温莎无法解决它也就不足为奇了。

这根本不是温莎问题。

关于c# - CaSTLe Windsor - 将泛型实现解析为基类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3039296/

相关文章:

generics - Ada 通用程序

caSTLe-windsor - 温莎城堡拦截机 - 订单?

c# - 每次 IsCallback 变得 fasle

c# - 对特定 Google Apps 域的 MVC4/Google OpenID 限制

对具有无界通配符类型的方法参数使用泛型 lambda 的 Java 编译错误

java - 泛型:上限通配符转换/转换错误

asp.net-mvc - 使用 CaSTLe Windsor 在 ASP.NET MVC 中实现 Multi-Tenancy 的最佳实践是什么?

asp.net - 如何注册通用存储库的依赖?

C#构造函数问题

C#:抽象类需要实现接口(interface)?