在c#中优先使用泛型版本的非泛型方法

时间:2014-10-31 14:52:29

标签: c# generics nhibernate justmock

我试图使用JustMock来存根NHibernate ICriteria。

具体来说,我试图将List<>存根使用对象数组调用它时的方法:

var mockCriteria = Mock.Create<ICriteria>();
Mock.Arrange(() => mockCriteria.List<object[]>()).Returns(
    new object[]
    {
        new object[] {"CompanyX", 1, 1, 1, 0}, 
        new object[] {"CompanyX", 1, 1, 1, 0}, 
        new object[] {"CompanyY", 2, 1, 1, 0}
    });

当我执行第二行(安排)时,我收到错误:

System.InvalidOperationExceptionSystem.Collections.IList List() is not a GenericMethodDefinition. MakeGenericMethod may only be called on a method for which MethodBase.IsGenericMethodDefinition is true.
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at \x7\x5\x2.\x8\x2.\x3\x15\x2(Type \x8, MethodBase , BindingFlags \x1C\xF\x2)   at \x6\x2.\x6\x2.Create(Object , MethodInfo \x6, Boolean \x6\x2)   at \x7\x5\x2.\x11\x14\x2.\x11\x13\x2(\xF\x2 \x5\xF)   at Telerik.JustMock.Mock.\x1F.\x15\x2(\x11\x14\x2 \x2)   at \x7\x5\x2.\x5\x14\x2.\x1C[\x4\x14\x2,\x5\x2](\xF\x2 \xF\x2, Func`2 \x3\x14\x2)   atThreshold.DeviceManagerGateway.UnitTests.Queries.DeviceNetworkStatusQueryFacts.RetrieveDevicesAsDeviceNetworkStats() in DeviceNetworkStatusQueryFacts.cs: line 24

ICriteria同时使用List()List<T>()方法,看起来编译器正在拾取List方法的非泛型版本而不是通用版本版。假设我正确并且它正在选择错误版本的List方法,有人知道如何强迫这个吗?或者,如果它是一个不同的问题,任何人都可以指出我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我尝试使用最新版本的JustMock,问题不会出现。

从混淆的存在来看,似乎你使用的是旧版本。

您可以从NuGet或从Telerik帐户下载最新版本的JustMock。

相关问题