如何对自定义类型使用Visual Studio Text Visualizer? [英] How to use Visual Studio Text Visualizer for custom types?
问题描述
在Visual Studio 2015(和某些较旧的版本)中,当调试C#代码时,可以通过带有下拉菜单的下拉列表在各种可视化工具(文本,XML,HTML,JSON)中显示string
变量的值.放大镜图标.这也适用于某些非字符串类型,例如System.Xml.Linq.XElement
.是否可以使用这些内置的可视化器来显示我自己的自定义类型的变量的值?
In Visual Studio 2015 (and in some older versions) when debugging C# code it's possible to display the values of the string
variables in various visualizers (Text, XML, HTML, JSON) via a drop-down list with a magnifying glass icon. This also works for some non-string types, for example, System.Xml.Linq.XElement
. Is it possible to use these build-in visualizers to display the value of a variable of my own custom type?
上下文:
我需要能够快速检查仅在多行文本环境中才能可视化的复杂自定义类型的状态.
I need to be able to quickly check the state of a complicated custom type that can be acceptably visualized only in a multi-line text environment.
推荐答案
If I understand your question correctly, then you can achieve what you're after with a DebuggerTypeProxy. It causes the debugger to create and display a proxy object whenever you're inspecting objects of your complex type.
在下面的示例中,代理对象包含一个(多行)字符串属性,您可以使用文本可视化工具对其进行查看.如果您仍然需要查看基础对象本身,那么这就是Raw view
按钮的作用.
In the example below the proxy object contains a (multi-line) string property that you can view with the text visualizer. If you still need to look at the underlying object itself, then that's what the Raw view
button is for.
[DebuggerTypeProxy(typeof(ComplexTypeProxy))]
class ComplexType
{
// complex state
}
class ComplexTypeProxy
{
public string Display
{
get { return "Create a multi-line representation of _content's complex state here."; }
}
private ComplexType _content;
public ComplexTypeProxy(ComplexType content)
{
_content = content;
}
}
这篇关于如何对自定义类型使用Visual Studio Text Visualizer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!