为什么 Visual Studio 调试器无法正确计算涉及泛型类型参数的表达式?
在以下代码中:
private static void Main(string[] args)
{
var listy = new List<DateTime> { DateTime.Now };
MyMethod(listy);
}
static void MyMethod<T>(List<T> myList)
{
// put breakpoint here
}
如果我中断调试器,在“myList”上打开 QuickWatch,我会看到:
myList
[0]
Raw View
如果我选择“[0]”节点并单击添加监视,则添加到监视的表达式:
(new System.Collections.Generic.Mscorlib_CollectionDebugView
此表达式似乎正确,但监视窗口显示以下错误:
最佳重载方法匹配 'System.Collections.Generic.Mscorlib_CollectionDebugView.Mscorlib_CollectionDebugView(System.Collections.Generic.ICollection)' 有一些无效参数
这似乎是调试器中的一个错误。为什么会出现这种情况?它是否记录在任何地方?
In the following code:
private static void Main(string[] args)
{
var listy = new List<DateTime> { DateTime.Now };
MyMethod(listy);
}
static void MyMethod<T>(List<T> myList)
{
// put breakpoint here
}
If I break in the debugger, open QuickWatch on "myList", I see:
myList
[0]
Raw View
If I select the "[0]" node and click Add Watch, the expression that is added to Watch:
(new System.Collections.Generic.Mscorlib_CollectionDebugView<System.DateTime>(myList)).Items[0]
This expression seems correct, and yet, the watch window shows the following error:
The best overloaded method match for
'System.Collections.Generic.Mscorlib_CollectionDebugView.Mscorlib_CollectionDebugView(System.Collections.Generic.ICollection)'
has some invalid arguments
This seems like a bug in the debugger. Why does this happen? And is it documented anywhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来像是 C# 表达式求值器的重载解析逻辑中的错误。调用泛型类型构造函数和传递绑定泛型的组合似乎是关键。删除其中任何一个似乎都可以解决问题。例如,您可以通过将
myList
显式转换为ICollection
来调用提到的表达式(这并不能解决我尝试过的所有情况)这是我编写的示例程序缩小问题
范围 同时,您可以尝试以下评估
Example(myList)
- 正常工作,不会出现错误new C(myList)
-失败并出现相同的错误此时我认为您应该在 Connect 上提交错误。这绝对是一个错误(类似的代码在 VB.Net 中工作正常)
This looks like a bug in the C#'s expression evaluator's overload resolution logic. The combination of invoking a generic type constructor and passing a bound generic seems to be a key. Removing either of these seems to fix the problem. For example you can invoke the expression mentioned by explicitly casting
myList
toICollection<DateTime>
(this doesn't fix all cases I tried though)Here's a sample program I wrote to narrow down the problem
At the same break you can try the following evaluations
Example(myList)
- Works without errornew C<DateTime>(myList)
- Fails with the same errorAt this point i think you should file a bug on Connect. It's definitely a bug (similar code works fine in VB.Net)
看起来是这样。我已经能够复制该错误。
Mscorlib_CollectionDebugView
只有一个接受ICollection
的构造函数,而List
实现ICollection
>。此外,显式转换为ICollection
也有效:Looks that way. I've been able to replicate the error.
Mscorlib_CollectionDebugView<T>
has only one constructor acceptingICollection<T>
andList<T>
implementsICollection<T>
. Also, explicitly casting toICollection<T>
works: