[英]How can I get the DisplayString field from Debugger.GetExpression?
I'm writing a Visual Studio Extension in C# that executes code in the Immediate Window while debugging another application.我正在用 C# 编写一个 Visual Studio 扩展,它在调试另一个应用程序时在立即窗口中执行代码。 The expression returns a value that can be a int, a string literal, a class, etc. The application that I'm debugging is written in C++.该表达式返回的值可以是 int、字符串文字、类等。我正在调试的应用程序是用 C++ 编写的。
To execute the Immediate Window commands I use this line of code:要执行即时窗口命令,我使用以下代码行:
var expression = dte.Debugger.GetExpression("funcname()");
And to retrieve the result I use:并检索我使用的结果:
string val = expression.Value;
and:和:
var children = expression.DataMembers;
Here is myFunc() in the application I'm debugging:这是我正在调试的应用程序中的 myFunc():
std::vector<int> myFunc()
{
return { 1, 2, 3, 4, 5 };
}
When I manually run the expression in the Immediate Window, the returned object is dumped as I see it in the Watch Window ( see here ).当我在立即窗口中手动运行表达式时,返回的对象将被转储,就像我在监视窗口中看到的一样(请参阅此处)。 I managed to find all the children names, but the values are missing ( see here ).我设法找到了所有孩子的名字,但缺少值(请参阅此处)。
What I want is to achieve the DisplayString ( { size=5 }
), but I haven't found anything useful yet.我想要的是实现 DisplayString ( { size=5 }
),但我还没有发现任何有用的东西。
How can I get the DisplayString field from Debugger.GetExpression?如何从 Debugger.GetExpression 获取 DisplayString 字段?
EDIT: I don't have to use this API.编辑:我不必使用此 API。 If you know another way that can return this sting, please suggest it.如果您知道可以返回此刺痛的另一种方法,请提出建议。 One idea could be retrieving the full output string of the Immediate Window ( see the right side ), and then parsing it.一种想法可能是检索立即窗口的完整输出字符串(见右侧),然后对其进行解析。
EDIT2: See this video that better explains the issue EDIT2:看这个视频,更好地解释了这个问题
you used a vector variable to get/parse the function return value, it's reasonable, because the return result of myFunc() is exactly a vector type, thus the variable “std::vector result” can parse and get the vector object correctly.你用vector变量来获取/解析函数返回值,这是合理的,因为myFunc()的返回结果正好是一个vector类型,因此变量std::vector result可以正确解析和获取vector对象。 What do you mean by “ manually run the expression in the Immediate Window, the returned object is dumped as I see it in the Watch Window”? “在立即窗口中手动运行表达式,返回的对象将被转储,就像我在观察窗口中看到的那样”是什么意思?
Whilst, EnvDTE.Expression.Value is a string representing the value of the object.同时,EnvDTE.Expression.Value 是一个表示对象值的字符串。 It has nothing to do with the displaystring '{ size=5 }', it's just a property expression.它与显示字符串 '{size=5 }' 无关,它只是一个属性表达式。 You can print it with below sample code:您可以使用以下示例代码打印它:
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);
}
Thus, it should be by-design we are not able to get '{size=5}' from the Immediate Window here:因此,应该是设计使然,我们无法从此处的立即窗口中获取 '{size=5}':
Regarding “the display string defined in the visualizer file (.natvis)” , DisplayString should be the property customized, could you provide your visualizer file (.natvis), or a sample project which can reproduce the issue?关于“在可视化文件(.natvis)中定义的显示字符串”,DisplayString应该是自定义的属性,您能否提供您的可视化文件(.natvis),或者可以重现问题的示例项目?
I found out what the problem was: I wasn't passing the UseAutoExpandRules
(defaulted to false) in GetExpression
.我发现了什么问题:我是不是经过UseAutoExpandRules
(默认为false)在GetExpression
。 Now my code works as expected!现在我的代码按预期工作!
Thanks to @Mia Wu-MSFT who put dte.Debugger.GetExpression("tempC", true, 1)
in her code and made me discover the UseAutoExpandRules
argument感谢@Mia Wu-MSFT 将dte.Debugger.GetExpression("tempC", true, 1)
放入她的代码中并让我发现了UseAutoExpandRules
参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.