Since beginning to use Code Contracts (.NET 4.0, VS2010 Ultimate & Premium) we have encountered a strange problem in the debugger windows.
I have a simple solution with one executable, one library that uses Code Contracts, and one library that doesn't. Each library contains one class and in the constructor of the class it initializes a list of integers. If we place a breakpoint after the list is initialized and look at it in the debugger windows (including the immediate window) we can see the values just fine. However, if we try to cast the value to its explicit type it breaks.
In the libraries that are not using Code Contracts the following works in the debugger windows:
(List<int>)nums
In the libraries that are using Code Contracts this generates "The type or namespace 'List' is not valid in this scope'. We have to do the following to get it to work:
(System.Collections.Generic.List<int>)nums
Please note that the code works fine, no issues there, the problem is only in the debugger windows.
UPDATE: The only setting that seems to have an effect is the check-box next to "Perform Runtime Contract Checking" on the "Code Contracts" tab in the project properties. After looking at the code in ILSpy, I found an attribute added to assemblyinfo.cs, an attribute (RuntimeContractsAttribute), and an enum (RuntimeContractsFlags), added to my project. On a whim, I copied the code for these items from ILSpy and created my own version. Now, everything works. But, when I select "Build" a contract reference assembly, the build fails. (I assume that the reference generator is trying to add the compiler generated code, and bombs cause I added it manually.) However, without adding the Runtime code manually, the debugger still fails regardless of the Build Contract Reference Assembly setting.