简体   繁体   English

Visual Studio Natvis DisplayString条件

[英]Visual Studio Natvis DisplayString Conditionals

Using Visual Studio 2017, I'm writing a Visualizer for some classes, but I'm running into issues with respect to the .natvis code readability. 使用Visual Studio 2017,我正在为某些类编写Visualizer,但是我遇到了关于.natvis代码可读性的问题。 I have a custom Array type, and I would like to display its members in the Watch window's Value field directly (as opposed to having to use the expand button). 我有一个自定义数组类型,我想直接Watch窗口的Value字段中显示其成员(而不是必须使用展开按钮)。

arrayType is an enum defining the array type (Sparse, Empty, Dense, Singleton). arrayType是一个定义数组类型的枚举(Sparse,Empty,Dense,Singleton)。 numElems is the number of entries in the array. numElems是数组中的条目数。 No need to focus on this data structure; 无需关注这种数据结构; it's just an example. 这只是一个例子。 Here is my current natvis code: 这是我目前的natvis代码:

<Type Name="MyProject::MyArray"
  <DisplayString Condition="numElems == 0">
    {arrayType,  en}
  </DisplayString>
  <DisplayString Condition="numElems == 1">
    {arrayType,  en} {*elems[0]}
  </DisplayString>
  <DisplayString Condition="numElems == 2">
    {arrayType,  en} {*elems[0]}, {*elems[1]}
  </DisplayString>
  <DisplayString Condition="numElems == 3">
    {arrayType,  en} {*elems[0]}, {*elems[1]}, {*elems[2]}
  </DisplayString>
  <DisplayString Condition="numElems >= 4">
    {arrayType,  en} {*elems[0]}, {*elems[1]}, {*elems[2]}, ...
  </DisplayString>
</Type>

[Notice that I have to repeat the same code over again (just adding an extra element to display). [请注意,我必须重复相同的代码(只需添加一个额外的元素来显示)。 I'm stopping at displaying 3 elements due to the code bloat.] 由于代码膨胀,我停止显示3个元素。

This would allow the Watch Window (within Visual Studio) to display objects in the following manner: 这将允许Watch Window (在Visual Studio中)以下列方式显示对象:

Name       Value
arr1       Sparse 5, 3                         ; numElems is 2
arr2       Empty                               ; numElems is 0
arr3       Singleton 1                         ; numElems is 1
arr4       Dense 58, 23, 1, ...                ; numElems is >= 4

Ideally, the DisplayString tag would allow conditionals within it. 理想情况下,DisplayString标记将允许其中的条件。 I've tried the following workaround with C++ ternary operators, which did not work. 我已尝试使用C ++三元运算符进行以下解决方法,但这些运算符无效。

<Type Name="MyProject::Array"
  <DisplayString>
    {arrayType,  en} {(numElems >= 1) ? *elems[0] : ""}, {(numElems >= 2) ? *elems[1] : ""}, {(numElems >= 3) ? *elems[2] : ""}, {(numElems >= 4) ? "..." : ""}
  </DisplayString>
</Type>

Notice that in this code, I don't have to repeat the DisplayString tag multiple times over and duplicate code. 请注意,在此代码中,我不必多次重复DisplayString标记并重复代码。 Maybe there is a way to provide an iterator within the DisplayString so as to list all the (variable amount of) elements? 也许有一种方法可以在DisplayString中提供一个迭代器,以便列出所有(可变数量的)元素? I know about the ArrayItems tag, but that must be used within an Expand tag (again, I'm trying to display the array elements in the Value field (without having to click the expand button)). 我知道ArrayItems标签,但必须在Expand标签中使用(同样,我试图在Value字段中显示数组元素(无需单击展开按钮))。

Any suggestions? 有什么建议? Need clarification? 需要澄清吗? Thanks! 谢谢!

It is not possible. 这不可能。 What you already have is the only way to implement what you want (multiple DisplayStrings with conditionals). 你已经拥有的是实现你想要的唯一方法(带有条件的多个DisplayStrings)。

你也可以使用{elems,[numElems]},这很简单,但也显示指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM