为什么代码合同显示“合同格式错误”。分配后发现要求”在带有 params keywork 的方法中?
我已经解决这个错误几个小时了,我似乎无法理解为什么会发生这种情况。考虑以下代码:
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 => x.TestData1, x => x.TestData2));”导致以下编译错误:
合同格式错误。在方法“Contracts.Program.Method(Contracts.Data)”中分配后找到 Requires。
为什么“Contract.Requires(Methods.TestMethod2(“test1”,“test2”));”不会导致错误,但“Contract.Requires(Methods.TestMethod4(d, x => x.TestData1, x => x.TestData2));”做?
请帮忙! :(
I've been troubleshooting with this error for hours and I can't seem to understand why this happens. Consider the following code:
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; }
}
}
When I compile the project, the line "Contract.Requires(Methods.TestMethod4(d, x => x.TestData1, x => x.TestData2));" causes the following compilation error:
Malformed contract. Found Requires after assignment in method 'Contracts.Program.Method(Contracts.Data)'.
How come "Contract.Requires(Methods.TestMethod2("test1", "test2"));" doesn't cause an error but "Contract.Requires(Methods.TestMethod4(d, x => x.TestData1, x => x.TestData2));" does?
Please help! :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 MSDN 论坛上发布了该问题,并且 他们也认为这也是一个错误。
I posted the problem on the MSDN forums and they think it's a bug too.