为什么 Visual Studio 调试器无法正确计算涉及泛型类型参数的表达式?

发布于 2024-11-29 20:46:52 字数 867 浏览 0 评论 0原文

在以下代码中:

        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(myList)).Items[0]

此表达式似乎正确,但监视窗口显示以下错误:

最佳重载方法匹配 '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 技术交流群。

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

发布评论

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

评论(2

浮生未歇 2024-12-06 20:46:52

这看起来像是 C# 表达式求值器的重载解析逻辑中的错误。调用泛型类型构造函数和传递绑定泛型的组合似乎是关键。删除其中任何一个似乎都可以解决问题。例如,您可以通过将 myList 显式转换为 ICollection 来调用提到的表达式(这并不能解决我尝试过的所有情况)

这是我编写的示例程序缩小问题

class C<T> {
    public C(ICollection<T> collection) {

    }
}

static void Example<T>(ICollection<T> collection) {
}

范围 同时,您可以尝试以下评估

  • 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 to ICollection<DateTime> (this doesn't fix all cases I tried though)

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
  • new C<DateTime>(myList) - Fails with the same error

At this point i think you should file a bug on Connect. It's definitely a bug (similar code works fine in VB.Net)

夏末的微笑 2024-12-06 20:46:52

看起来是这样。我已经能够复制该错误。 Mscorlib_CollectionDebugView 只有一个接受 ICollection 的构造函数,而 List 实现 ICollection >。此外,显式转换为 ICollection 也有效:

(new System.Collections.Generic.Mscorlib_CollectionDebugView<System.DateTime>((ICollection<DateTime>)myList)).Items[0]

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>. Also, explicitly casting to ICollection<T> works:

(new System.Collections.Generic.Mscorlib_CollectionDebugView<System.DateTime>((ICollection<DateTime>)myList)).Items[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文