在调试时在Watch Window中查看Exception.Data

Jer*_*n K 8 .net c# debugging visual-studio visual-studio-debugging

在Visual Studio中进行调试时,如何在Watch窗口中轻松查看Exception的Data属性的内容?它是奇怪的类型System.Collections.ListDictionaryInternal.

我想你可以分别看到键和值:

        try {
            ... do something that throws exception with Data
        }
        catch (Exception ex) {
            throw;
        }
        finally {
        }
Run Code Online (Sandbox Code Playgroud)

在Watch窗口中:

ex.Data.Keys.Cast<string>()
ex.Data.Values.Cast<string>()
Run Code Online (Sandbox Code Playgroud)

但你能把它看作字典还是什么?

Ome*_*viv 10

System.Collections.ListDictionaryInternal 是一个IDictionary,因此您只需在Watch或QuickWatch窗口中评估以下表达式:

new System.Collections.Hashtable(ex.Data)
Run Code Online (Sandbox Code Playgroud)

编辑:我共同创建了一个名为OzCode的Visual Studio商业扩展,使这更容易.有了它,您可以简单地将鼠标悬停在Exception变量上,右键单击它,选择Create Custom Expression,然后键入new System.Collections.Hashtable([obj].Data) // Data.从那一刻开始,无论何时查看异常,您都可以以一种格式良好的方式查看其数据字典,而无需任何手动步骤,如下所示: 截图