Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code contracts build reference assembly actions

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?

like image 627
oleksii Avatar asked Apr 28 '11 10:04

oleksii


People also ask

What are the code contracts?

Code Contracts provide a language-agnostic way to express coding assumptions in . NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs.

Which of the following methods is used in pre conditions and post conditions for code contracts in net?

NET 4.0. Code Contracts API includes classes for static and runtime checks of code and allows you to define preconditions, postconditions, and invariants within a method.

What is a assembly reference in C#?

Reference assemblies are usually distributed with the Software Development Kit (SDK) of a particular platform or library. Using a reference assembly enables developers to build programs that target a specific library version without having the full implementation assembly for that version.


2 Answers

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 as if (!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 selected Build 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.

like image 80
David Schwartz Avatar answered Oct 04 '22 07:10

David Schwartz


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.

like image 40
Henk Holterman Avatar answered Oct 04 '22 07:10

Henk Holterman