代码合同似乎不适用于VS2012

时间:2022-10-18 20:53:33

I'm reading up on Code Contracts, which at first glance seem to be pretty revolutionary, but I can't seem to get them working. I'm running Windows 8 and Visual Studio 2012 Premium (Release versions of both). I then installed Code Contracts from here by clicking on the Download Code Contracts link.

我正在阅读代码契约,乍一看似乎是相当革命性的,但我似乎无法让它们发挥作用。我正在运行Windows 8和Visual Studio 2012 Premium(两者的发行版本)。然后,我通过单击“下载代码合同”链接从此处安装了代码合同。

I then wrote the following code in a brand new console app:

然后我在一个全新的控制台应用程序中编写了以下代码:

class Program
{
   static void Main(string[] args)
   {
      var answer = Add(0, 5);
      Console.Write(answer);

      Console.ReadLine();
   }

   static int Add(int x, int y)
   {
      Contract.Requires(x > 0 && y > 0);

      return x + y;
   }
}

I expect the compilation to fail, since the first parameter of Add is 0, but the program succeeds and prints 5 out to the console.

我希望编译失败,因为Add的第一个参数是0,但是程序成功并向控制台输出5。

I've tried with the default Code Contract settings, and also mucked with stuff a bit to no avail. My current settings look like this:

我已尝试使用默认的代码合约设置,并且还使用了一些东西而无济于事。我当前的设置如下所示:

代码合同似乎不适用于VS2012

Any ideas what I'm doing wrong?

我有什么想法我做错了吗?

UPDATE:

更新:

Here's the results from the Build window. It appears it's doing something, but just raising warnings instead of errors. In the video I watched, these things got flagged as compile errors and the program wouldn't even run.

这是Build窗口的结果。看起来它正在做某事,但只是提出警告而不是错误。在我观看的视频中,这些内容被标记为编译错误,程序甚至无法运行。

1>------ Build started: Project: DeleteMe, Configuration: Debug Any CPU ------
1>  DeleteMe -> c:\users\mike\documents\visual studio 2012\Projects\DeleteMe\DeleteMe\bin\Debug\DeleteMe.exe
1>  CodeContracts: Task manager is unavailable.
1>  CodeContracts: DeleteMe: Run static contract analysis.
1>  CodeContracts: Suggested requires: Contract.Requires(false);
1>  CodeContracts: DeleteMe: Validated:  0.0 %
1>  CodeContracts: DeleteMe: Contract density: 0.87
1>  CodeContracts: DeleteMe: Total methods analyzed 4
1>  CodeContracts: DeleteMe: Methods with 0 warnings 3
1>  CodeContracts: DeleteMe: Total time 4.974sec. 1243ms/method
1>  CodeContracts: DeleteMe: Methods with necessary preconditions: 1
1>  CodeContracts: DeleteMe: Discovered 1 new candidate preconditions in 00:00:00.1718843
1>  CodeContracts: DeleteMe: Retained 1 preconditions after filtering
1>  CodeContracts: DeleteMe: Inferred 0 object invariants
1>  CodeContracts: DeleteMe: Retained 0 object invariants after filtering
1>  CodeContracts: DeleteMe: Detected 0 code fixes
1>  CodeContracts: DeleteMe: Proof obligations with a code fix: 0
1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(14,10,14,33): warning : CodeContracts: requires is false: x > 0 && y > 0
1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(22,10,22,44): warning : CodeContracts: location related to previous warning
1>  CodeContracts: Checked 1 assertion: 1 false
1>  CodeContracts: DeleteMe: 
1>  CodeContracts: DeleteMe: Static contract analysis done.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

2 个解决方案

#1


13  

So, the issue seemed to be a combination of several limitations and gotchas with Code Contracts. Hopefully this answer will help people like me just starting out.

因此,这个问题似乎是代码合同的几个限制和陷阱的组合。希望这个答案能帮助像我这样的人刚开始。

First off, Code Contracts does support Visual Studio 2012 (any version other than Express) since build 1.4.50327.0, though you have to run devenv.exe /setup if your build is older than 1.4.50910.0. See the Release Notes for more information.

首先,代码合同确实支持Visual Studio 2012(除了Express之外的任何版本),因为如果你的版本早于1.4.50910.0,你必须运行devenv.exe / setup。有关更多信息,请参阅发行说明。

The first issue I was having was that the "Cache Results" check box was checked in the "Static Checking" section of the Code Contracts properties tab. This option is on by default, and also requires SQL Server CE to store its cached data, which is not installed by Windows 8, VS2012 or Code Contracts. Unfortunately, your program will continue to compile just fine, and you'd have to manually go dig through the Build output to see the error:

我遇到的第一个问题是在“代码约定”属性选项卡的“静态检查”部分中选中了“缓存结果”复选框。默认情况下,此选项处于启用状态,并且还要求SQL Server CE存储其缓存数据,这些数据未由Windows 8,VS2012或Code Contracts安装。不幸的是,你的程序将继续编译得很好,你必须手动浏览Build输出才能看到错误:

CodeContracts: xxx: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

CodeContracts:xxx:未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集'System.Data.SqlServerCe,Version = 3.5.1.0,Culture = neutral,PublicKeyToken = 89845dcd8080cc91'或其依赖项之一。该系统找不到指定的文件。

Unchecking the "Cache Results" checkbox will fix this issue, as would installing SQL Server CE.

取消选中“缓存结果”复选框将解决此问题,就像安装SQL Server CE一样。

The second issue is that Code Contract violations are treated as warnings, and not compile errors. Even if you have "Treat Warnings as Errors" enabled, your program will continue to compile and run successfully. If you have a larger project with tons of Warnings you ignore, it could potentially be difficult to notice these new Code Contract warnings. In the demo video I saw, these warnings were also reflected in the Visual Studio IDE (the calling code had a blue underline), however I don't seem to get this behavior in Visual Studio 2012.

第二个问题是Code Contract违规被视为警告,而不是编译错误。即使您启用了“将警告视为错误”,您的程序也将继续编译并成功运行。如果您有一个较大的项目,您忽略了大量的警告,则可能很难注意到这些新的“代码合同”警告。在我看到的演示视频中,这些警告也反映在Visual Studio IDE中(调用代码有一个蓝色下划线),但我似乎没有在Visual Studio 2012中获得此行为。

This design decision disturbs me. If I define a contract within my code, that a function must take an integer greater than 0, and I blatantly pass in a 0, this is an error. Not a warning. I broke that contract, plain and simple.

这个设计决定让我感到不安。如果我在我的代码中定义一个契约,那么一个函数必须取一个大于0的整数,并且我公然传入一个0,这是一个错误。不是警告。我违反了合同,简单明了。

Overall, I'd say Code Contracts is extremely powerful, and could potentially change the way we test software. MS Research definitely did great work. However, I don't think it's really ready for mainstream yet. It takes some tweaking to get working, it doesn't seamlessly integrate into the Visual Studio build process, and it's also pretty slow. On smaller projects, it worked as expected, but when I plugged it in to a larger project, it took a good ten minutes to analyze all the code.

总的来说,我认为Code Contracts非常强大,可能会改变我们测试软件的方式。 MS Research绝对做得很棒。但是,我认为它还没有为主流做好准备。它需要一些调整才能工作,它不能无缝集成到Visual Studio构建过程中,而且速度也很慢。在较小的项目上,它按预期工作,但当我将其插入更大的项目时,花了十分钟时间来分析所有代码。

#2


2  

Sir, you will have to enable runtime checking. Under your in the window you posted as an image.

先生,您必须启用运行时检查。在您在窗口中,您发布为图像。

"Full" will mean that all conditions will be checked. The rest are self explanatory.

“完整”意味着将检查所有条件。其余的都是自我解释的。

  • Static checking only runs in the background and checks for any thing it can improve within your code. Caution - it slows your builds down somewhat.
  • 静态检查仅在后台运行,并检查代码中可以改进的任何内容。注意 - 它会使您的构建速度有所降低。

#1


13  

So, the issue seemed to be a combination of several limitations and gotchas with Code Contracts. Hopefully this answer will help people like me just starting out.

因此,这个问题似乎是代码合同的几个限制和陷阱的组合。希望这个答案能帮助像我这样的人刚开始。

First off, Code Contracts does support Visual Studio 2012 (any version other than Express) since build 1.4.50327.0, though you have to run devenv.exe /setup if your build is older than 1.4.50910.0. See the Release Notes for more information.

首先,代码合同确实支持Visual Studio 2012(除了Express之外的任何版本),因为如果你的版本早于1.4.50910.0,你必须运行devenv.exe / setup。有关更多信息,请参阅发行说明。

The first issue I was having was that the "Cache Results" check box was checked in the "Static Checking" section of the Code Contracts properties tab. This option is on by default, and also requires SQL Server CE to store its cached data, which is not installed by Windows 8, VS2012 or Code Contracts. Unfortunately, your program will continue to compile just fine, and you'd have to manually go dig through the Build output to see the error:

我遇到的第一个问题是在“代码约定”属性选项卡的“静态检查”部分中选中了“缓存结果”复选框。默认情况下,此选项处于启用状态,并且还要求SQL Server CE存储其缓存数据,这些数据未由Windows 8,VS2012或Code Contracts安装。不幸的是,你的程序将继续编译得很好,你必须手动浏览Build输出才能看到错误:

CodeContracts: xxx: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

CodeContracts:xxx:未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集'System.Data.SqlServerCe,Version = 3.5.1.0,Culture = neutral,PublicKeyToken = 89845dcd8080cc91'或其依赖项之一。该系统找不到指定的文件。

Unchecking the "Cache Results" checkbox will fix this issue, as would installing SQL Server CE.

取消选中“缓存结果”复选框将解决此问题,就像安装SQL Server CE一样。

The second issue is that Code Contract violations are treated as warnings, and not compile errors. Even if you have "Treat Warnings as Errors" enabled, your program will continue to compile and run successfully. If you have a larger project with tons of Warnings you ignore, it could potentially be difficult to notice these new Code Contract warnings. In the demo video I saw, these warnings were also reflected in the Visual Studio IDE (the calling code had a blue underline), however I don't seem to get this behavior in Visual Studio 2012.

第二个问题是Code Contract违规被视为警告,而不是编译错误。即使您启用了“将警告视为错误”,您的程序也将继续编译并成功运行。如果您有一个较大的项目,您忽略了大量的警告,则可能很难注意到这些新的“代码合同”警告。在我看到的演示视频中,这些警告也反映在Visual Studio IDE中(调用代码有一个蓝色下划线),但我似乎没有在Visual Studio 2012中获得此行为。

This design decision disturbs me. If I define a contract within my code, that a function must take an integer greater than 0, and I blatantly pass in a 0, this is an error. Not a warning. I broke that contract, plain and simple.

这个设计决定让我感到不安。如果我在我的代码中定义一个契约,那么一个函数必须取一个大于0的整数,并且我公然传入一个0,这是一个错误。不是警告。我违反了合同,简单明了。

Overall, I'd say Code Contracts is extremely powerful, and could potentially change the way we test software. MS Research definitely did great work. However, I don't think it's really ready for mainstream yet. It takes some tweaking to get working, it doesn't seamlessly integrate into the Visual Studio build process, and it's also pretty slow. On smaller projects, it worked as expected, but when I plugged it in to a larger project, it took a good ten minutes to analyze all the code.

总的来说,我认为Code Contracts非常强大,可能会改变我们测试软件的方式。 MS Research绝对做得很棒。但是,我认为它还没有为主流做好准备。它需要一些调整才能工作,它不能无缝集成到Visual Studio构建过程中,而且速度也很慢。在较小的项目上,它按预期工作,但当我将其插入更大的项目时,花了十分钟时间来分析所有代码。

#2


2  

Sir, you will have to enable runtime checking. Under your in the window you posted as an image.

先生,您必须启用运行时检查。在您在窗口中,您发布为图像。

"Full" will mean that all conditions will be checked. The rest are self explanatory.

“完整”意味着将检查所有条件。其余的都是自我解释的。

  • Static checking only runs in the background and checks for any thing it can improve within your code. Caution - it slows your builds down somewhat.
  • 静态检查仅在后台运行,并检查代码中可以改进的任何内容。注意 - 它会使您的构建速度有所降低。