我已经对这个错误进行了几个小时的故障排除,我似乎无法理解为什么会发生这种情况。请考虑以下代码:
using System;
using System.Diagnostics.Contracts;
using System.Linq.Expressions;
namespace Contracts
{
class Data
{
public object TestData1 { get; set; }
public object TestData2 { get; set; }
}
class Program
{
static void Main()
{
Data d = new Data();
Method(d);
}
static void Method(Data d)
{
Contract.Requires(Methods.TestMethod1("test"));
Contract.Requires(Methods.TestMethod2("test1", "test2"));
Contract.Requires(Methods.TestMethod3(d, x => x.TestData1));
Contract.Requires(Methods.TestMethod4(d, x => x.TestData1, x => x.TestData2));
}
}
static class Methods
{
[Pure]
public static bool TestMethod1(string str) { return true; }
[Pure]
public static bool TestMethod2(params string[] strs) { return true; }
[Pure]
public static bool TestMethod3<T>(T obj, Expression<Func<T, object>> exp) { return true; }
[Pure]
public static bool TestMethod4<T>(T obj, params Expression<Func<T, object>>[] exps) { return true; }
}
}
当我编译项目时,行“Contract.Requires(Methods.TestMethod4(d,x =&gt; x.TestData1,x =&gt; x.TestData2));”导致以下编译错误:
合同格式错误。在方法'Contracts.Program.Method(Contracts.Data)'中找到后需要。
为什么“Contract.Requires(Methods.TestMethod2(”test1“,”test2“));”不会导致错误但是“Contract.Requires(Methods.TestMethod4(d,x =&gt; x.TestData1,x =&gt; x.TestData2));”呢?
请帮忙! :(