合同.需要使用

发布于 2024-12-18 13:38:47 字数 1050 浏览 3 评论 0原文

这是我的问题。我是合同设计的忠实粉丝,我使用这个概念,特别是在开发可供其他开发人员使用的库时。我刚刚找到了一种新的方法,即:Contract.Requires 而不是 Exception: 因此,

public void SomeMethod(string name){
   if(name==null) throw new NullArgumentException("Null values not supported");
} 

我现在有:

public void SomeMethod(string name){
   Contract.Requires(name != null);
}

编辑:我正在 VS2010 的调试模式下工作。

问题:即使 namenullContract.Requires 也不会执行任何操作!

MSDN 文档说:

指定封闭方法的前提条件契约或 财产。

但没有指定任何内容,以防不满足条件!

我还注意到还有 其他 Contract.Requires抛出异常、显示消息的 重载...但是 Contract.Requires(Boolean) 的用途是什么?

编辑下面的答案强调了必须安装插件才能拥有 Contract API 的全部功能,但是如果 Mono 用户希望自己的代码在不同的平台上表现相同,该怎么办?平台?

Here is my problem. I am a very big fan of Design by contract, I am using this concept especially when developing libraries that can be used by other developers. I just found out a new way of doing this which is: Contract.Requires instead of Exception:
So instead of having:

public void SomeMethod(string name){
   if(name==null) throw new NullArgumentException("Null values not supported");
} 

I now have:

public void SomeMethod(string name){
   Contract.Requires(name != null);
}

EDIT: I am working under VS2010 on debug mode.

Problem: Contract.Requires does not do anything, even when name is null!

The MSDN documentation says:

Specifies a precondition contract for the enclosing method or
property.

But nothing is specified in case the condition is not met!

I also noticed there are other Contract.Requires overloads that throw exception, display message... but then what is Contract.Requires(Boolean) for?

EDIT Answer below highlighted that a plug-in must be installed to have the full power of Contract API but then what about Mono users who want their code to behave the same on different platforms?

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

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

发布评论

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

评论(3

虚拟世界 2024-12-25 13:38:47

您应该执行以下操作:

  1. 按照 nfechner 的说明安装代码合同加载项
  2. 转到项目属性的“代码合同”文件夹
  3. 选中“执行运行时合同检查”
  4. 将“汇编模式”切换为“标准合同需要”
  5. 替换您的 Contract.RequiresContract.Requires (第一个抛出System.Diagnostics.ContractException 而第二个抛出您指定的异常,这对于公共方法很重要)

这是基本设置。为了更准确的配置,参考手册

如果你使用Mono,可能是Contract类是空的。我还没有这样做,但是合同手册中的第七章似乎解释了如何提供您自己的实现。

You should do the following:

  1. Install the Code Contracts add-in as nfechner has noted
  2. Go to the project properties, 'Code Contracts' folder
  3. Check 'Perform Runtime Contract Checking'
  4. Switch 'Assembly Mode' to 'Standard Contract Requires'
  5. Substitute your Contract.Requires with Contract.Requires<SomeException> (the first one throws System.Diagnostics.ContractException while the second throws the exception you specified which is important for public methods)

That's the basic setup. For more accurate configuration, refer to the manual

If you use Mono, probably, Contract class is empty. I haven't done this, but chapter seven from the Contracts manual seems to explain how to provide your own implementation.

与往事干杯 2024-12-25 13:38:47

来自合同类文档

重要

您必须安装 Visual Studio 加载项才能强制执行合同。 代码
Contracts Premium Edition
插件可让您指定静态和运行时
在项目属性页面上检查代码合同。如果你这样做
不启用运行时检查,诸如 之类的合同合同.保证
如果合约是这样的,方法在运行时不会抛出异常
违反了。 Visual Studio 加载项不随 Visual Studio 一起提供
2010 或 Windows SDK。

From the Contract class docs:

Important

You must install a Visual Studio add-in to enforce contracts. The Code
Contracts Premium Edition
add-in lets you specify static and run-time
checking of code contracts on the project Properties page. If you do
not enable run-time checking, contracts such as the Contract.Ensures
method will not throw exceptions during run time if a contract is
violated. The Visual Studio add-in does not ship with Visual Studio
2010 or the Windows SDK.

雾里花 2024-12-25 13:38:47

通过这样的消息,准确说明您所做的事情通常会很有帮助。

例如,您没有在原始消息中提及是否已安装 VS Addon,也没有提及您已在项目属性下启用它,或者您实际上是在调试与发布模式下运行,等等。Re

Contract。 RequiresContract.Requires

推荐使用 。
根据说明书

如果您的代码必须在失败时抛出特定异常
特定的前提条件,您可以使用通用重载形式
以下。 (在您提交此表格之前,请先阅读第 5.1 节
代码。您不能使用 Requires < Exn> 无需运行合约工具
在所有构建上。如果这样做,每次都会遇到运行时失败。)

With a message like this it is usually helpful to specify exactly what you have done.

For example, you do not mention in the original message if you have installed the VS Addon, nor that you have enabled it under your project properties, or that you are actually running in debug vs release mode, etc.

Re Contract.Requires vs Contract.Requires<Exception>

Contract.Requires is recommended.
According to the manual

If your code must throw a particular exception on failure of a
particular precondition, you can use the generic overloaded form
below. (Please read Section 5.1 before committing to this form in your
code. You cannot use Requires < Exn <Exn>> without running the contract tools
on all builds. If you do, you will get a runtime failure everytime.)

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