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 initi开发者_开发知识库alized 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.
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.
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.
精彩评论