.Net 代码合约 - 哪里可以了解更多信息?

发布于 2024-12-20 13:37:29 字数 1539 浏览 0 评论 0原文

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

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

发布评论

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

评论(2

豆芽 2024-12-27 13:37:29

代码契约是在 .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:

定格我的天空 2024-12-27 13:37:29

代码契约是一种对函数的输入和输出执行检查的相对较新的方法。它们与标准 Assert 类型检查的不同之处在于,生成的检查输入的 IL 在调用函数之前直接检查输入,而检查输出的代码则在函数实际退出后进行检查。

为什么这有用?

好吧,它可以防止您在认为函数可能返回后修改变量,从而可能引入错误。

这是一个例子。

public void doSomething(SomeObject foo)
{
    Contract.Requires<ArgumentNullException>(foo != null);
}

现在,代码契约要求在检查之前没有代码。在生成的 IL 中,foo 的值在调用之前进行测试。这是确保您的输入符合预期的可靠方法。

另一个是Contract.Ensures 构造。这基本上类似于 Requires 但对您的返回值进行操作。

public int doSomethingElse()
{
    Contract.Ensures(Contract.Result<int>() != 0);
    int ret = 1;
    return ret;
}

如果您的函数有多个退出路径,这将特别有用......

public int someBadFunction()
{
    Contract.Ensures(Contract.Result<int>() != 0);
    if(....)
    {
       if(....) return 2;
       if(....) return 8;
    }
    return 3;
}

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.

public void doSomething(SomeObject foo)
{
    Contract.Requires<ArgumentNullException>(foo != null);
}

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 like Requires but operates on your return value.

public int doSomethingElse()
{
    Contract.Ensures(Contract.Result<int>() != 0);
    int ret = 1;
    return ret;
}

This would be particularly useful if you had multiple exit paths from your function...

public int someBadFunction()
{
    Contract.Ensures(Contract.Result<int>() != 0);
    if(....)
    {
       if(....) return 2;
       if(....) return 8;
    }
    return 3;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文