代码契约构建参考装配操作
我正在使用代码契约,并试图了解我应该使用哪些构建选项以及何时使用。 中定义
合同程序集构建选项在项目属性代码合同 -> 。合同参考汇编:
- 无
- 构建
- DoNotBuild
有什么想法或建议吗?
I am using code contracts and trying to understand which of the build options shall I use and when. The contract assembly build options are defined in project properties
Code Contracts -> Contract Reference Assembly:
- None
- Build
- DoNotBuild
Any thoughts or recommendations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
合同参考程序集是一种特殊的程序集,它保留您在源代码文件中定义的任何代码合同。这是必要的,因为在编译时,代码合约的“重写器”(
ccrewriter
) 会删除或用等效的验证代码替换每个合约 (Contract.Requires(someBool)
可能会重写为if (!someBool) throw
)。如果没有代码契约,如果您稍后在不同的解决方案中引用已编译的程序集(不是项目及其所有源代码文件),它可能不会意识到任何代码契约。如果创建了合约参考程序集,IDE 就可以在静态分析期间考虑该程序集中的任何合约。
至于设置,其含义如下:
(none)
表示您尚未进行选择,因此不会创建任何参考程序集。如果另一个程序集依赖于此程序集,并且您已为其选择构建
,则您可能会收到一条错误/警告:“未找到合同引用程序集”。如果您将设置更改为
Build
,则会创建一个包含所有合约的参考程序集。您将能够使用该程序集中定义的所有代码协定,就像您拥有源代码一样。如果您要创建一个将由第 3 方(例如 NuGet 包)或任何人在编译程序集后使用的库,请选择此选项,这样他们就可以从您的静态代码契约中受益如果您将设置更改为
DoNotBuild
,则不会构建任何参考程序集来保留您的代码协定。如果您不打算在其他任何地方使用该程序集,或者该程序集的所有其他用户都可以访问源代码并且不需要引用程序集,请选择此选项。它可能会稍微加快构建速度。The Contract Reference Assembly is a special kind of assembly which preserves any code contracts you defined in your source code files. This is necessary because at compile-time, the code contracts' "rewriter" (
ccrewriter
) removes or replaces each contract with equivalent verification code (Contract.Requires(someBool)
might be rewritten asif (!someBool) throw
).Without the code contracts, if you later reference the compiled assembly (not the project and all it's source code files) in a different solution, it may not be aware of any of the code contracts. Had a Contract Reference Assembly been created, the IDE could take into account any contracts in that assembly during static analysis.
As for the settings, here's what they mean:
(none)
means you have not made a selection, and so no reference assembly will be created. If another assembly depends on this one and you have selectedBuild
for it, you may receive an error/warning that "no contract reference assembly was found."If you change the setting to
Build
, a reference assembly will be created that contains all of your contracts. You will be able to use all of the code contracts defined in that assembly as if you had the source code. Choose this if you are creating a library that will be used by a 3rd party (a NuGet package, for example) or anyone after the assembly is compiled so they will have the benefit of your code contracts in static analysis.If you change the setting to
DoNotBuild
, no reference assembly will be built preserving your code contracts. Choose this if you don't intend for this assembly to be used anywhere else, or if all other users of the assembly will have access to the source code and not need the reference assembly. It may speed up the build a little.是的,None 和 DoNotBuild 选项看起来有点奇怪。
如果您选择“无”并在带有合同的项目中引用该库,您将收到警告。
如果您选择 DoNotBuild,您将不会收到警告。
当然,只有 Build 会生成参考组件,对于 .EXE 来说,这一切都不重要。
Yes, the None and DoNotBuild options seem a bit strange.
If you select None and reference the library in a Project with contracts, you will get a Warning.
If you select DoNotBuild you won't get a warning.
And of course only Build produces a reference assy, and for a .EXE it all doesn't matter.