是否可以更改第 3 方类的 Visual Studio 调试器变量窗口值列中显示的内容?
调试时,有各种 变量 窗口(autos、locals、watch)包含列名称、值、类型。 该值通常似乎显示对象的类名称。在特定情况下,我想根据类实例的属性展示一些更有意义的内容。
作为一个具体示例,对于 CodeTypeReference 我想查看基于 BaseType 字符串或 ArrayElementType 值,而不是看到“System.CodeDom.CodeTypeReferenceExpression”。
可视化工具似乎提供单独的对话框窗口,而不是填充值列的方法。
数据提示是按变量而不是按类型。
最接近的似乎是 DebuggerTypeProxyAttribute 在这种情况下我想我'我问“是否可以将属性应用于其他人的类?”
我主要处理 Visual Studio 2010,尽管 Visual Studio 2008 答案会很有用。
When debugging there are various Variable windows (autos, locals, watch) containing columns Name,Value,Type.
The value often seems to show an object's class name. In specific cases I would like to show something more meaningful based on the properties of the class instance.
As a specific example, for CodeTypeReference I'd like to see a text representation of the type referenced (where valid) based on the BaseType string or ArrayElementType value, rather than seeing "System.CodeDom.CodeTypeReferenceExpression".
Visualizers seem to offer separate dialog windows, rather than a way to populate the value column.
Data tips are per variable rather than per type.
The closest thing seems to be the DebuggerTypeProxyAttribute in which case I guess I'm asking "Is it possible to apply an attribute to somebody else's class ?"
I'm mainly dealing with Visual Studio 2010 although a Visual Studio 2008 answer would be useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为自己的类重写 ToString 方法。 Value 通常只是对 ToString 的调用,如果没有被覆盖,这会显示类型。
更新:对于您没有代码的类,我无法提供有价值的答案,而不是您可以包装这些类(继承自),但是,嗯,有礼貌很奇怪(恕我直言)。
You can override the ToString method for your own classes. The Value is usually just a call to ToString, and if not overridden, this shows the type.
Update: For classes you do not have the code for, I cannot provide a valuable answer rather than you could wrap the classes ( inherit from ), but that is, hm, strange to be polite ( IMHO ).
是的,您可以将 DebuggerTypeProxyAttribute 应用于您不拥有的类型(除非它们是私有的),如我对类似问题的回答此处
Yes you can apply DebuggerTypeProxyAttribute to types you don't own (unless they are private) as explained in my answer to a similar question here
是的,有两种方法可以实现此目的:
通过使用 OzCode(以前为 Visual Studio 的 BugAid) ),我创建的一个商业工具,您只需星标您的类型的属性/字段,并让它们出现在任何类型的值列,无论是否是第 3 方。
您可以更改 autoexp.cs 文件,并应用 DebuggerDisplayAttribute 或 DebuggerTypeProxyAttribute 为第 3 方类型,如 我对类似问题的回答。例如,您可以通过以下方式将 DebuggerDisplayAttribute 应用于 System.Drawing.Pen:
[ assembly: DebuggerDisplay(@"\{Color = {color}}", Target = typeof(Pen))]
这个答案适用于VS2008和VS2010。
Yes, there are two ways you can accomplish this:
By using OzCode (previously BugAid for Visual Studio), a commercial tool that I created, you can simply star properties/fields on your type, and have those appear in the Value column on any type, whether it is 3rd party or not.
You can change your autoexp.cs file, and apply either the DebuggerDisplayAttribute or the DebuggerTypeProxyAttribute to 3rd party types, as described in my answer on a similar question. For example, this is how you would apply the DebuggerDisplayAttribute to System.Drawing.Pen:
[assembly: DebuggerDisplay(@"\{Color = {color}}", Target = typeof(Pen))]
This answer applies to both VS2008 and VS2010.