为什么代码合同显示“合同格式错误”。分配后发现要求”在带有 params keywork 的方法中?

发布于 2024-11-18 04:36:39 字数 1683 浏览 3 评论 0原文

我已经解决这个错误几个小时了,我似乎无法理解为什么会发生这种情况。考虑以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜唯美灬不弃 2024-11-25 04:36:39

我在 MSDN 论坛上发布了该问题,并且 他们也认为这也是一个错误

I posted the problem on the MSDN forums and they think it's a bug too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文