如何从Debugger.GetExpression获取DisplayString字段? 背景代码问题

问题描述

背景

我正在用C#编写一个Visual Studio扩展,它在调试另一个应用程序时在即时窗口中执行代码。该表达式返回的值可以是int,字符串文字,类等。我正在调试的应用程序是用C ++编写的。

代码

要执行立即窗口命令,我使用以下代码行:

var expression = dte.Debugger.GetExpression("funcname()");

要检索结果,我使用:

string val = expression.Value;

和:

var children = expression.DataMembers;

这是我正在调试的应用程序中的myFunc():

std::vector<int> myFunc()
{
    return { 1,2,3,4,5 };
}

问题

当我在“立即窗口”中手动运行表达式时,返回的对象将按照我在“监视窗口”(see here)中看到的方式转储。我设法找到了所有子代的名称,但是值丢失了(see here)。

我想要实现displayString({ size=5 }),但是我还没有发现任何有用的东西。

如何从Debugger.GetExpression获取displayString字段?

编辑:我不必使用此API。如果您知道另一种可以返回这种this的方法,请提出建议。一种想法可能是检索立即窗口(see the right side)的完整输出字符串,然后对其进行解析。

EDIT2:See this video that better explains the issue

解决方法

你用vector变量来获取/解析函数返回值,这是合理的,因为myFunc()的返回结果正好是向量类型,因此变量std::vector result可以解析得到向量对象正确。 “在立即窗口中手动运行表达式,返回的对象将被转储,就像我在观察窗口中看到的那样”是什么意思?

同时,EnvDTE.Expression.Value 是一个表示对象值的字符串。它与显示字符串‘{size=5}’无关,它只是一个属性表达式。您可以使用以下示例代码打印它:

public static void Value(DTE dte)  
{  
    // Setup debug Output window.  
    Window w = (Window)dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);  
    w.Visible = true;  
    OutputWindow ow = (OutputWindow)w.Object;  
    OutputWindowPane owp = ow.OutputWindowPanes.Add("Value property: ");  
    owp.Activate();  

    EnvDTE.Expression exp = dte.Debugger.GetExpression("tempC",true,1);  
    owp.OutputString("\nThe name of the expression: " + exp.Name);  
    owp.OutputString("\nThe type of the expression: " + exp.Type);  
    owp.OutputString("\nThe value of the expression: " + exp.Value);  
}

因此,我们无法从此处的立即窗口中获取“{size=5}”,这应该是故意的: enter image description here

关于“在可视化文件(.natvis)中定义的显示字符串”,DisplayString应该是自定义的属性,你能提供你的可视化文件(.natvis),或者可以重现问题的示例项目吗?

,

我发现了问题所在:我没有在 UseAutoExpandRules 中传递 GetExpression(默认为 false)。现在我的代码按预期工作了!

感谢@Mia Wu-MSFT 将 dte.Debugger.GetExpression("tempC",1) 放入她的代码中并让我发现了 UseAutoExpandRules 参数