了解EndContractBlock方法 [英] Understanding of EndContractBlock method

查看:267
本文介绍了了解EndContractBlock方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!请考虑以下示例。我看到重写器在核心方法之外重定位if / then / throw语句,就像正常的前提条件一样。但是,当我取消选中"执行运行时验证"时,不会跳过验证。项目选项
设置。

Hi! Consider the following sample. I see that the rewriter relocates the if/then/throw statement outside of the core method, like a normal precondition. However the validation is not skipped when I uncheck the "Perform runtime validation" option on the project settings.

        public void FillTank(int volume)
        {
            if (volume <= 0) throw new ArgumentOutOfRangeException("volume""Cannot fill with nothing");
            Contract.EndContractBlock();
 
            this.FuelLevel += volume;
        }

所以我的问题如下。 EndContractBlock 方法有什么意义?为什么重写器重新定位此代码,因为即使保留原样,它也会表现相同。

So my question is the following. What's the point of EndContractBlock method? Why the rewriter relocates this code, since it would behave the same even if it was left as is.

推荐答案

EndContractBlock()方法是一个标记,以帮助我们的工具了解前提条件结束的位置和"正常"的标准。方法体开始了。在调用EndContractBlock()之前,我们的工具对代码施加了许多限制,因此这有助于程序员理解
它们之前可以拥有的内容以及它们之后必须留下的内容。重写器将if-then-throw更改为前置条件,以便其行为可以由指定给重写器的选项控制,就像所有其他合同一样。例如,你可以将
更改为导致断言对话而不是抛出异常。

The EndContractBlock() method is a marker to help our tools understand where the preconditions end and the "normal" method body begins. There are many restrictions our tools impose on code before the call to EndContractBlock() so this helps programmers understand what they can have before it and what they must leave until after it. The rewriter changes the if-then-throw into a precondition so that its behavior can be controlled by the options specified to the rewriter, just as all of the other contracts do. For instance, you could change it into causing an assertion dialog instead of throwing an exception.

这有帮助吗?


这篇关于了解EndContractBlock方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆