Is it possible to change what is displayed in a Visual Studio Debugger Variables window Value column for a 3rd party class?

安稳与你 提交于 2019-12-13 17:16:55

问题


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.


回答1:


Yes, there are two ways you can accomplish this:

  1. 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.

  2. 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.




回答2:


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 ).




回答3:


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



来源:https://stackoverflow.com/questions/7660160/is-it-possible-to-change-what-is-displayed-in-a-visual-studio-debugger-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!