在 Visual Studio 2010 中设置代码合同
因此,我尝试使用代码契约运行一些简单的代码(我有一段时间没有使用过),
static void Main(string[] args)
{
double res = sqrt(-5);
}
static double sqrt(int a)
{
Contract.Requires(a >= 0, "a must be >= 0!");
return Math.Sqrt(a);
}
但当我运行它时,它似乎根本没有做任何事情。根据我几个月前的记忆,它应该会抛出一个关于 a
小于 0 的错误。
为了安全起见,我从 Code Contracts 站点重新安装了学术版本,但这似乎仍然不正确正在工作。让代码契约发挥作用需要采取哪些步骤?
编辑:Resharper 在 Contract.Requires(a >= 0);
上告诉我该方法被跳过,因为它是“有条件的或没有实现的部分方法”。
谢谢
So I was trying to run some simple code with Code Contracts (that I haven't used for some time)
static void Main(string[] args)
{
double res = sqrt(-5);
}
static double sqrt(int a)
{
Contract.Requires(a >= 0, "a must be >= 0!");
return Math.Sqrt(a);
}
But it doesn't seem to do anything at all when I run it. From what I recall from some months ago, it should throw up an error about a
being less than 0.
I reinstalled the Academic version from the Code Contracts site just to be safe and this still doesn't seem to be working. What are the steps needed to put Code Contracts to work?
EDIT: Resharper is telling me on Contract.Requires(a >= 0);
that the method is being skipped, as it is either "conditional or a partial method without implementation".
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该检查此网页:链接文本
您应该转到项目属性、“代码合同”选项卡,然后单击“运行时”复选框。
You should check this web page: link text
You should go to the project properties, Code Contracts tab and click the Runtime checkboxes.