让代码契约在 Visual Studio 2010 中工作
我有以下代码:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(SqrtRoot(0));
Console.WriteLine(SqrtRoot(10));
Console.WriteLine(SqrtRoot(-10));
Console.ReadKey();
}
public static int SqrtRoot(int i)
{
Contract.Requires(i >= 0);
return (int)Math.Sqrt(i);
}
}
我在调试模式下运行它,它应该在最后一行中引发某种错误
Console.WriteLine(SqrtRoot(-10));
,但由于某种原因,它没有。它似乎忽略了 Contract.Requires() 调用。尝试使用代码契约时我应该设置一些东西吗?
我正在使用 Visual Studio 2010 RC。
谢谢
I have the following code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(SqrtRoot(0));
Console.WriteLine(SqrtRoot(10));
Console.WriteLine(SqrtRoot(-10));
Console.ReadKey();
}
public static int SqrtRoot(int i)
{
Contract.Requires(i >= 0);
return (int)Math.Sqrt(i);
}
}
I am running it in debug mode, and it should fire some kind of error in the last line
Console.WriteLine(SqrtRoot(-10));
altough, for some reason, it doesn't. It seems to ignore the Contract.Requires() call. Should I set up something when trying to use Code Contracts?
I'm using Visual Studio 2010 RC.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要安装 Visual Studio 集成。虽然 CodeContracts 库本身是 .NET 4 的一部分,但您的代码需要由 Code Contracts 重写器 (
ccrewrite
) 重写才能实际正确使用该库。从 DevLabs 站点 下载安装程序。
You need to install the Visual Studio integration. While the CodeContracts library itself is part of .NET 4, your code needs to be rewritten by the Code Contracts rewriter (
ccrewrite
) to actually use the library properly.Download the installer from the DevLabs site.
我也没有该选项卡,但我找到了解决方法:
可能必须安装代码合同包,但下载链接不起作用。 http://msdn.microsoft.com/en-us/devlabs/dd491992。 ASPX
I don't have that tab either but I found a workaround:
Probably the code contracts package must be installed but the download link is not working. http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx
我认为您必须在项目设置中启用运行时合同检查(应该有一个“代码合同”窗格...)
请参阅 用户文档(第 6 节)了解更多信息。
I think you have to enable runtime contract checking in the project settings (there should be a "Code Contracts" pane...)
See the user documentation (section 6) for further information.