简体   繁体   English

Visual Studio Express 2013中的代码约定支持

[英]Code Contracts support in Visual Studio Express 2013

I've been developing a C# project in Visual Studio Express 2013 and came across Code Contracts for .NET languages. 我一直在Visual Studio Express 2013中开发一个C#项目,并且遇到了.NET语言的代码契约 Impressed by their brevity and the static analysis tool that came with them, I started using them in my code base. 他们的简洁性和随附的静态分析工具给我留下了深刻的印象,我开始在我的代码库中使用它们。 However, when I attempted to run my program, I was met by an error message similar to the one found in this SO thread , ie 但是,当我试图运行我的程序时,我遇到了类似于在这个SO线程中发现的错误消息,即

...An assembly (probably "<my project>") must be rewritten using the code contracts binary rewriter (CCRewrite) because it is calling Contract.Requires and the CONTRACTS_FULL symbol is defined. ...必须使用代码契约二进制重写器(CCRewrite)重写程序集(可能是“<my project>”),因为它正在调用Contract.Requires并且定义了CONTRACTS_FULL符号。 Remove any explicit definitions of the CONTRACTS_FULL symbol from your project and rebuild... 从项目中删除CONTRACTS_FULL符号的任何显式定义并重建...

Guides suggest that to fix this, I have to enable Code Contracts from my project's Properties page, but a Code Contracts Properties entry is nowhere to be found in Express. 指南建议要解决此问题,我必须从项目的“属性”页面启用代码约定,但在Express中无法找到代码约定属性条目。

Some MSDN forum threads seem to indicate that all the tooling for Code Contracts is included in the Express version, but the Code Contracts Properties page is not. 一些MSDN论坛主题似乎表明代码合同的所有工具都包含在Express版本中,但Code Contracts Properties页面却没有。 This seems to be the case, as I was able to run my project in VSE 2013 only after enabling Code Contracts with a copy of Visual Studio 2012 Ultimate I acquired from my university before graduating. 这似乎是这种情况,因为我能够在VSE 2013中运行我的项目,只有在我的大学毕业之前从我的大学获得的Visual Studio 2012 Ultimate的副本启用代码合同之后。

Is there really no way to work with Code Contracts in Visual Studio Express except by modifying the project files either manually or with a paid version of Visual Studio? 除了通过手动修改项目文件或使用付费版本的Visual Studio修改项目文件外,是否真的无法在Visual Studio Express中使用代码约定? If this is the case, I am extremely hesitant to use them at all, since my company is unlikely to purchase VS licenses. 如果是这种情况,我对使用它们非常犹豫,因为我的公司不太可能购买VS许可证。 Futhermore, it seems extremely odd that Microsoft would attempt to proliferate this new and superior verification paradigm but then restrict it to paying customers only. 此外,微软试图扩展这种新的优越的验证范例,然后将其仅限于付费用户,这似乎是非常奇怪的。

You can probably use the new VS Community 2013, as long as you meet the licensing requirements: http://www.visualstudio.com/en-us/visual-studio-community-vs 只要您符合许可要求,您就可以使用新的VS Community 2013: http//www.visualstudio.com/en-us/visual-studio-community-vs

Here's how Visual Studio Community can be used in organizations: 以下是Visual Studio社区在组织中的使用方式:

An unlimited number of users within an organization can use Visual Studio Community for the following scenarios: in a classroom learning environment, for academic research, or for contributing to open source projects. 组织中的无限数量的用户可以将Visual Studio社区用于以下场景:在课堂学习环境中,用于学术研究,或用于开源项目。

For all other usage scenarios: In non-enterprise organizations, up to 5 users can use Visual Studio Community. 对于所有其他使用方案:在非企业组织中,最多5个用户可以使用Visual Studio社区。 In enterprise organizations (meaning those with >250 PCs or > $1 Million US Dollars in annual revenue), no use is permitted beyond the open source, academic research, and classroom learning environment scenarios described above. 在企业组织中(意味着那些拥有超过250台个人电脑或年收入超过100万美元的企业),除了上述开源,学术研究和课堂学习环境之外,不允许使用它们。

It's basically VS 2013 Professional for free so you can install the code contracts extension. 它基本上是免费的VS 2013 Professional,因此您可以安装代码合同扩展。

The problem is your edition of Visual Studio. 问题是您的Visual Studio版本。 You are running Visual Studio 2013 Express edition, as you stated. 正如您所说,您正在运行Visual Studio 2013 Express版。 In order to use the binary re-writer from within Visual Studio, you need to install the Code Contracts extensions. 要在Visual Studio中使用二进制重写器,您需要安装Code Contracts扩展。 These you can download from Microsoft's Research in Software Engineering (RiSE) site, which is packaged as a Windows Installer. 这些可以从Microsoft的软件工程研究(RiSE)站点下载,该站点打包为Windows Installer。

The installer installs the binary re-writer that is required, as well as a bunch of Visual Studio extensions. 安装程序会安装所需的二进制重写器以及一堆Visual Studio扩展。 Unfortunately, Code Contracts states in their manual on page 40 that they don't support the various Visual Studio Express editions. 遗憾的是,Code Contracts在其手册 (第40页)中指出,它们不支持各种Visual Studio Express版本。 You'll need to at least have Visual Studio 2013 Professional edition in order to use the Code Contracts binary re-writer extension from within Visual Studio, and in order to see the Code Contracts tab on the project properties window. 您需要至少拥有Visual Studio 2013 Professional版本才能在Visual Studio中使用Code Contracts二进制重写器扩展,并且可以在项目属性窗口中查看Code Contracts选项卡。

Code Contracts performs assembly re-writing as a post-build step. 代码约定执行程序集重写作为构建后步骤。 Meaning, Visual Studio first compiles the .NET code as it normally would. 这意味着,Visual Studio首先会像往常一样编译.NET代码。 But, when using Code Contracts and enabling the right options on your project (assuming the VS extensions are installed—which you can't do in Express editions), then the binary re-writer is called for you by Visual Studio after the normal compilation process. 但是,当使用代码约定并在项目上启用正确的选项时(假设安装了VS扩展 - 在Express版本中无法做到),那么在正常编译之后Visual Studio会为您调用二进制重写器处理。

Instead, after compiling your program, you'll need to manually run the ccrewrite program installed by Code Contracts on the compiled assembly (and all dependent assemblies) to 're-write' your assemblies, which will add in all the Code Contract checking information. 相反,在编译程序之后,您需要手动运行编译程序集(和所有相关程序集)上的Code Contracts安装的ccrewrite程序来“重写”程序集,这将添加所有代码合同检查信息。 See the Code Contracts documentation (also available on the RiSE website) for information on how to do this. 有关如何执行此操作的信息,请参阅代码合同文档(也可在RiSE网站上获得)。

Contracts are supposed to be useless for Visual Studio Express 2013 (at least Code Contracts come with a plugin for all non-express versions). 对于Visual Studio Express 2013来说,合同应该是无用的(至少代码合同附带了一个适用于所有非快递版本的插件)。

So one has to define the symbol CONTRACTS_FULL in project properties -> build -> general -> conditional compilation symbols . 因此,必须在项目属性中定义符号CONTRACTS_FULL - > build - > general - >条件编译符号

And then add the proper call to ccrewrite.exe to the post-build event command line found under project properties -> build events . 然后ccrewrite.exe的正确调用添加到项目属性 - >构建事件下的后构建事件命令行。 The command should be set to something like 该命令应设置为类似的

"C:\\Program Files (x86)\\Microsoft\\Contracts\\Bin\\ccrewrite.exe" -throwOnFailure "$(TargetPath)"

I successfully used this tonight with NUnit. 我今晚用NUnit成功地使用了它。 Using contracts, my unit tests don't need to check for post conditions explicitly. 使用合同,我的单元测试不需要明确检查后置条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM