Code Contracts doesn't seem to work on VS2012

后端 未结 2 1333
天命终不由人
天命终不由人 2021-02-18 18:18

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 P

相关标签:
2条回答
  • 2021-02-18 18:53

    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.
    0 讨论(0)
  • 2021-02-18 19:02

    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.

    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:

    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.

    Unchecking the "Cache Results" checkbox will fix this issue, as would installing 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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题