2

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.

basilard99
  • 307
  • 2
  • 12

2 Answers2

0

Yes, this is a known problem. There is some debugging information in the assembly's pdb that we probably don't maintain correctly, having to do with the set of usings in scope at a given point. This affects what the debugger will assume is in scope and thus the problem you are encountering.

Manuel Fahndrich
  • 665
  • 4
  • 12
0

The difference between the two libraries that matters is not whether or not they are using the Code Contracts library, but rather whether you have a using System.Collections.Generic; statement in the code file in which the debugger is breaked on.

The Visual Studio debugger does its best to evaluate expressions as if you were to write that expression on the line of code in which you are breaking on, and that includes respecting the lexical scope of that line of code; if you were to try to cast to List<int> in a file that doesn't have a using System.Collections.Generic;, that would fail with an error, too.

Omer Raviv
  • 11,409
  • 5
  • 43
  • 82
  • OK, I'm wrong then :) I'm guessing this has something to do with Code Contracts rewriting of the IL. I think the answer might be the same as in http://stackoverflow.com/questions/3191319/code-contracts-for-net-3-5-messes-up-vs10s-debugger ; maybe you should try opening a Connect issue for it? – Omer Raviv Jul 08 '11 at 15:23
  • I saw that answer earlier (thanks for the link), but his problem was solved when he upgraded. One of the devs opened a Connect issue, but we had to mark it private as the original issue contained business information :(. Thanks for your input, I do really appreciate it. – basilard99 Jul 08 '11 at 15:31