[英]Why won't Visual Studio debugger properly evaluate expressions that involve generic type arguments?
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”上打开 QuickWatch,我看到:
myList
[0]
Raw View
If I select the "[0]" node and click Add Watch, the expression that is added to Watch:如果我在 select 的“[0]”节点上单击 Add Watch,即添加到 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:这个表达式似乎是正确的,然而,手表 window 显示以下错误:
The best overloaded method match for 'System.Collections.Generic.Mscorlib_CollectionDebugView.Mscorlib_CollectionDebugView(System.Collections.Generic.ICollection)' has some invalid arguments
'System.Collections.Generic.Mscorlib_CollectionDebugView.Mscorlib_CollectionDebugView(System.Collections.Generic.ICollection)'的最佳重载方法匹配有一些无效的 ZDBC161CAA58BDA2F77
This seems like a bug in the debugger.这似乎是调试器中的一个错误。 Why does this happen?
为什么会这样? And is it documented anywhere?
它是否记录在任何地方?
This looks like a bug in the C#'s expression evaluator's overload resolution logic.这看起来像是 C# 表达式求值器的重载解析逻辑中的一个错误。 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
to ICollection<DateTime>
(this doesn't fix all cases I tried though)例如,您可以通过将
myList
显式转换为ICollection<DateTime>
来调用提到的表达式(但这并不能解决我尝试过的所有情况)
Here's a sample program I wrote to narrow down the problem这是我为缩小问题范围而编写的示例程序
class C<T> {
public C(ICollection<T> collection) {
}
}
static void Example<T>(ICollection<T> collection) {
}
At the same break you can try the following evaluations在同一休息时间,您可以尝试以下评估
Example(myList)
- Works without error Example(myList)
- 正常工作new C<DateTime>(myList)
- Fails with the same error new C<DateTime>(myList)
- 失败并出现同样的错误At this point i think you should file a bug on Connect .在这一点上,我认为您应该在Connect上提交错误。 It's definitely a bug (similar code works fine in VB.Net)
这绝对是一个错误(类似的代码在 VB.Net 中运行良好)
Looks that way.看起来是这样的。 I've been able to replicate the error.
我已经能够复制错误。
Mscorlib_CollectionDebugView<T>
has only one constructor accepting ICollection<T>
and List<T>
implements ICollection<T>
. Mscorlib_CollectionDebugView<T>
只有一个构造函数接受ICollection<T>
和List<T>
实现ICollection<T>
。 Also, explicitly casting to ICollection<T>
works:此外,显式转换为
ICollection<T>
有效:
(new System.Collections.Generic.Mscorlib_CollectionDebugView<System.DateTime>((ICollection<DateTime>)myList)).Items[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.