在升级到v3.0.0之后Castle ComponentRegistration错误
问题描述:
我继承了一个使用Castle Windsor IoC容器的代码库,最近因为另一个兼容性问题,我们最近*从3.0.0升级v2.5.2版本。在升级到v3.0.0之后Castle ComponentRegistration错误
继升级到V3.0.0,在我们的测试类下面的扩展方法失败,出现以下错误编译:
类型“TInterface”必须是引用类型才能使用它作为参数“TService”在泛型类型或方法“Castle.MicroKernel.Registration.ComponentRegistration”
public static class ContainerExtensions
{
/// <summary>
/// Sets the registration expectation on the mocked container.
/// </summary>
/// <typeparam name="TInterface">The type of the interface.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <param name="container">The container.</param>
public static void SetRegistrationExpectation<TInterface, TImplementation>(this IWindsorContainer container) where TImplementation : TInterface
{
Predicate<IEnumerable<IRegistration>> pred = regs =>
{
var reg = regs.OfType<ComponentRegistration<TInterface>>().FirstOrDefault();
return reg != null && reg.Implementation == typeof(TImplementation);
};
container
.Expect(c => c.Register(null))
.IgnoreArguments()
.Constraints(Rhino.Mocks.Constraints.Is.Matching(pred))
.Repeat.Once();
}
}
所以它似乎ComponentRegistration不再能的接口?看过文档后,我仍然不确定如何纠正?
任何指针,将不胜感激。
答
尝试where TInterface : class
约束添加到您的方法:
public static void SetRegistrationExpectation<TInterface, TImplementation>(this IWindsorContainer container)
where TInterface : class
where TImplementation : TInterface
好像在3.0改变ComponentRegistration
泛型约束。
非常感谢,做到了这一招 - 很简单,当你知道如何! – bigtv 2012-07-07 13:23:00