合同.需要使用
这是我的问题。我是合同设计的忠实粉丝,我使用这个概念,特别是在开发可供其他开发人员使用的库时。我刚刚找到了一种新的方法,即: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 的调试模式下工作。
问题:即使 name
为 null
,Contract.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该执行以下操作:
Contract.Requires
和Contract.Requires
(第一个抛出System.Diagnostics.ContractException 而第二个抛出您指定的异常,这对于公共方法很重要)这是基本设置。为了更准确的配置,参考手册
如果你使用Mono,可能是Contract类是空的。我还没有这样做,但是合同手册中的第七章似乎解释了如何提供您自己的实现。
You should do the following:
Contract.Requires
withContract.Requires<SomeException>
(the first one throwsSystem.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.
来自合同类文档:
From the Contract class docs:
通过这样的消息,准确说明您所做的事情通常会很有帮助。
例如,您没有在原始消息中提及是否已安装 VS Addon,也没有提及您已在项目属性下启用它,或者您实际上是在调试与发布模式下运行,等等。Re
Contract。 Requires
与Contract.Requires
推荐使用 。
根据说明书
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
vsContract.Requires<Exception>
Contract.Requires
is recommended.According to the manual