4

I installed a code contracts from original site and tried to write some sample code. But R# just writes that Method invocation is skipped. When I watch decompiled sources I see that method is conditional: CONTRACTS_FULL constant must be defined. I checked everything in the Code Contracts tab in project settings, but it doesn't seems to work.

How can it be fixed? enter image description here

Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151

2 Answers2

5

The user documentation(pdf) says this:

2 Contracts

Most methods of the contract class are conditionally compiled, meaning the compiler only emits calls to these methods when a special symbol, the full-contract symbol, is defined. That symbol is CONTRACTS_FULL. This allows writing contracts in your code without the use of #ifdef's, yet product different builds, some with contracts, and some without.

If you are using Visual Studio 2008 or later (Section 6) or msbuild (Section A.1), then you don't need to define this symbol yourself. Instead, when you use the provided UI to enable runtime or static checking (or properties in your projects or /p defines in msbuild arguments), the build automatically defines this symbol and performs the appropriate rewrite actions. If you use your own build mechanism, then you need to define the full-contract symbol if you want the contracts to be emitted into your assemblies for further consumption by tools.

So the code contract methods are conditionally compiled, based on the presence of CONTRACTS_FULL.

If you check Perform runtime contract checking or Perform static contract checking then Visual Studio will ensure that CONTRACTS_FULL is defined, but as a parameter passed to the build process instead of as a constant defined in the project. So checking those boxes is all you need to do to turn on the contract checking. (The alternative would be for those checkboxes to cause the CONTRACTS_FULL constant to be defined in the project, but then you'd have problems keeping the text field and its two checkboxes in sync.)

So, as far as any other tools (including Resharper) are concerned, the methods in Contract are conditional on a constant that isn't defined. You can either ignore the warnings or define the constant manually.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
-1

"Conditional constants" (more properly: "conditional compilation symbols") are set (or defined) in the Build tab of Project Properties.

Simply type "CONTRACTS_FULL" in the "Conditional compilation symbols" text field and you're all set.

Dai
  • 141,631
  • 28
  • 261
  • 374