使用 MS Code 合约时 VS2010 调试器窗口会丢失类型解析

发布于 2024-11-19 00:31:20 字数 800 浏览 3 评论 0原文

自从开始使用代码契约(.NET 4.0、VS2010 Ultimate 和 Premium)以来,我们在调试器窗口中遇到了一个奇怪的问题。

我有一个简单的解决方案,其中包含一个可执行文件、一个使用代码契约的库和一个不使用代码契约的库。每个库都包含一个类,并在该类的构造函数中初始化一个整数列表。如果我们在列表初始化后放置一个断点并在调试器窗口(包括立即窗口)中查看它,我们可以很好地看到这些值。但是,如果我们尝试将值转换为其显式类型,则会中断。

在不使用代码契约的库中,调试器窗口中会执行以下操作:

(List<int>)nums

在使用代码契约的库中,这会生成“类型或命名空间‘列表’在此范围内无效”。我们必须执行以下操作以让它工作:

(System.Collections.Generic.List<int>)nums

请注意,代码工作正常,没有问题,问题仅出现在调试器窗口中

更新:似乎有效的唯一设置是“执行运行时合同检查”旁边的复选框 。关于“代码合同”在查看 ILSpy 中的代码后,我发现在 assemblyinfo.cs 中添加了一个属性,一个属性(RuntimeContractsAttribute)和一个枚举(RuntimeContractsFlags),添加到我的项目中,我一时兴起复制了代码。对于来自 ILSpy 的这些项目并创建了我自己的版本。但是,当我选择“构建”合同引用程序集时,构建失败(我假设引用生成器正在尝试添加生成的编译器)。代码和炸弹,因为我手动添加了它。)但是,如果不手动添加运行时代码,无论构建合同参考程序集设置如何,调试器仍然会失败。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紙鸢 2024-11-26 00:31:20

这两个库之间重要的区别不在于它们是否使用 Code Contracts 库,而在于调试器所在的代码文件中是否有 using System.Collections.Generic; 语句被打破了。

Visual Studio 调试器会尽力计算表达式,就好像您要在要中断的代码行上编写该表达式一样,这包括尊重该代码行的词法范围;如果您尝试在没有 using System.Collections.Generic; 的文件中转换为 List,则会失败并出现错误,也。

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.

伴我老 2024-11-26 00:31:20

是的,这是一个已知问题。程序集的 pdb 中存在一些我们可能无法正确维护的调试信息,这些信息与给定点范围内的使用集有关。这会影响调试器假定的范围,从而影响您遇到的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文