Contract.Requires 和 DateTime
我有以下方法:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
静态验证器有其局限性,有时您必须提供帮助。它对数值数学的理解有限,但 DateTime 似乎超出了它的范围。
这就是
Contract.Assume(bool)
存在的原因: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: