1

我继承了一个使用 Castle Windsor IoC 容器的代码库,由于另一个兼容性问题,我们最近被迫从 3.0.0 升级 v2.5.2 版本。

升级到 v3.0.0 后,我们的一个测试类中的以下扩展方法无法构建,并出现以下错误:

类型“TInterface”必须是引用类型,才能将其用作泛型类型或方法“Castle.MicroKernel.Registration.ComponentRegistration”中的参数“TService”

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 不能再使用接口了?查看文档后,我仍然不确定如何纠正?

任何指针将不胜感激。

4

1 回答 1

1

尝试为where TInterface : class您的方法添加约束:

public static void SetRegistrationExpectation<TInterface, TImplementation>(this IWindsorContainer container)
    where TInterface : class
    where TImplementation : TInterface

It seems like ComponentRegistration generic constraints changed in 3.0.

于 2012-07-07T12:38:00.020 回答