Contract.Requires 和 DateTime

发布于 2024-11-08 15:23:15 字数 770 浏览 4 评论 0原文

我有以下方法:

private static void SampleMethod(DateTime dateTime1, DateTime dateTime2)
{
    Contract.Requires(dateTime1 > dateTime2);
    Console.WriteLine("date 1 > date 2");
}

SampleMethod(DateTime.Today, DateTime.Today.AddDays(1));

当我在启用静态检查的情况下构建它时,我收到警告 CodeContracts: require unproven: dateTime1 > dateTime2

请注意,动态检查对我来说工作得很好,它只是我遇到问题的静态检查。

我该如何证明这个断言,或者抑制警告?

编辑:

我正在阅读有关 ContractVerificationAttribute,如果我将 [ContractVefirication(false) 添加到方法中,则没有什么区别(我怀疑这可能是一个错误),但将其添加到类中将正确关闭静态检查对于全班来说。我仍在寻找一种方法来关闭该 Requires 的静态检查。

I have the following method:

private static void SampleMethod(DateTime dateTime1, DateTime dateTime2)
{
    Contract.Requires(dateTime1 > dateTime2);
    Console.WriteLine("date 1 > date 2");
}

SampleMethod(DateTime.Today, DateTime.Today.AddDays(1));

When I build it with static checking enabled, I get the warning CodeContracts: requires unproven: dateTime1 > dateTime2

Note that dynamic checking is working fine for me, its only the static checking I'm having a problem with.

How do I go about either proving this assertion, or else suppressing the warning?

EDIT:

I was reading about the ContractVerificationAttribute, if I add [ContractVefirication(false) to the method it makes no difference (I suspect this might be a bug) but adding it to the class will turn off static checking correctly for the whole class. Am still looking for a way to turn off static checking for that one Requires though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

晨光如昨 2024-11-15 15:23:15

静态验证器有其局限性,有时您必须提供帮助。它对数值数学的理解有限,但 DateTime 似乎超出了它的范围。

这就是 Contract.Assume(bool) 存在的原因:

    DateTime d1 = DateTime.Today;  
    DateTime d2 = d1.AddDays(-7);

    Contract.Assume(d1 > d2);

    SampleMethod(d1, d2);

CodeContracts:检查了 4 个断言:4 个正确

The static verifier has its limitations, sometimes you have to help. It has a limited understanding of numerical math but DateTime seems to be out of its scope.

That's why Contract.Assume(bool) exists:

    DateTime d1 = DateTime.Today;  
    DateTime d2 = d1.AddDays(-7);

    Contract.Assume(d1 > d2);

    SampleMethod(d1, d2);

CodeContracts: Checked 4 assertions: 4 correct

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