We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 9 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
代码契约是在 .NET 4.0 中引入的,它们提供了一种与语言无关的方法来表达程序中的编码假设。
它们基本上允许您检查前置条件、后置条件和其他功能,并且可以极大地改进测试过程和正在编写的代码的最终质量。
来自微软:
运行时检查。我们的二进制重写器通过注入来修改程序 合同,作为计划的一部分进行检查>重写的程序提高了可测试性:每个合约都充当预言机,为测试运行提供通过/失败指示。自动测试工具(例如 Pex)利用合约,通过过滤掉不满足先决条件的无意义测试参数来生成更有意义的单元测试。
静态检查。我们的静态检查器甚至无需运行程序即可确定是否存在任何合同违规!它检查隐式合同,例如 null 取消引用和数组边界,以及显式契约。
文档生成。我们的文档生成器使用合同信息扩充现有的 XML 文档文件。还有可与 Sandcastle 一起使用的新样式表,以便生成的文档页面具有合同部分。
了解更多信息:
Code Contracts were introduced in .NET 4.0 and they provide a language-agnostic method to express coding assumptions in programs.
They basically allow you to check for pre-conditions, post-conditions and other features and can greatly improve the testing process and the eventual quality of code that is being written.
From Microsoft:
Runtime Checking. Our binary rewriter modifies a program by injecting the contracts, which are checked as part of program> execution. Rewritten programs improve testability: each contract acts as an oracle, giving a test run a pass/fail indication. Automatic testing tools, such as Pex, take advantage of contracts to generate more meaningful unit tests by filtering out meaningless test arguments that don't satisfy the pre-conditions.
Static Checking. Our static checker can decide if there are any contract violations without even running the program! It checks for implicit contracts, such as null dereferences and array bounds, as well as the explicit contracts.
Documentation Generation. Our documentation generator augments existing XML doc files with contract information. There are also new style sheets that can be used with Sandcastle so that the generated documentation pages have contract sections.
Learn More:
代码契约是一种对函数的输入和输出执行检查的相对较新的方法。它们与标准 Assert 类型检查的不同之处在于,生成的检查输入的 IL 在调用函数之前直接检查输入,而检查输出的代码则在函数实际退出后进行检查。
为什么这有用?
好吧,它可以防止您在认为函数可能返回后修改变量,从而可能引入错误。
这是一个例子。
现在,代码契约要求在检查之前没有代码。在生成的 IL 中,
foo
的值在调用之前进行测试。这是确保您的输入符合预期的可靠方法。另一个是Contract.Ensures 构造。这基本上类似于
Requires
但对您的返回值进行操作。如果您的函数有多个退出路径,这将特别有用......
Code Contracts are a relatively new way of performing checks on input and output of functions. Where they differ from your standard
Assert
type checking is that the generated IL that checks input checks it directly prior to the function being called, and the code that checks output, after your function has actually exited.Why is this useful?
Well, it prevents you modifying the variables after you think your function may return, thereby potentially introducing bugs.
Here's an example.
Now, Code Contracts require that there be no code before that check. In the generated IL, the value of
foo
is tested PRIOR to the call. It's a sure-fire way of ensuring that your input is as expected.The other, is the
Contract.Ensures
construct. This is basically likeRequires
but operates on your return value.This would be particularly useful if you had multiple exit paths from your function...