开发者

Search in debug mode inside an object

Is it possible to search inside an object for values and/or other field while debugging a C# application? I'm looking for a deep 开发者_如何学编程search that can drill down the object for many levels.

What I'm looking for is a way to search (like F3 for documents search) inside very complex objects (while debugging, in the Quick watch window for example).


You could use OzCode (an add-on for Visual Studio), which lets you search for the members you want to see from within the on-hover-DataTip or the QuickWatch window.

See example in the following screenshot:

Search in debug mode inside an object

Full disclosure: I am the co-creator of "OzCode".


If you want to do whitout an add-on it can be done by going to the object list, selecting all objects (ctrl+a) and copy them to a searchable text-editor. This will give you a newline seperated list.

Search in debug mode inside an object


I think there is no built-in feature to lets you, dive in deep in the objects in debugging mode, Unless use extensions if available.


Besides Wilson Kao's post, you can also try the simplest approach that I use:

  1. Put a breakpoint at a place where a variable referencing the object will be used.
  2. Hover mouse over the variable when the breakpoint is hit. You should see a popup showing the top level properties of the object.
  3. Use the little + buttons on the left to go deeper into the object. It could get confusing.

You can also pin certain properties that you find deep down so you don't have to look for them again and again (pin button appears on the right side of each property when you hover over the property)

Here's an exmaple of what it looks like (the variable is books):

Search in debug mode inside an object

Source: http://blogesh.wordpress.com/2008/09/09/visual-studio-debugging-tips-and-tricks/

Edit: There is a way to get all the properties and their values that you access through the manual hover-and-click method (ie in the above image, you can get ISBN and its value through code). However you have to use Visual Basic code. You can then do a search on the results for a specific value.

First, you need to go read up on Tracepoints and custom Macros: Click and go to "TracePoints – Running a Custom Macro" Also, here

Next you need to make a Macro to do what you want. In Visual Studio, go to Tools -> Macros -> Macros IDE... Add a new Macro. There should be a preset blank one called Module 1. In here, you add routines to do what you want. The following is a Macro I made that will go through a certain variable and output the properties of it (basically, output the data you get in the cover-and-click method). This outputs to the Output window, which you can open through View -> Output.

    Sub DisplayAllProperties()
    Dim outputWindow As EnvDTE.OutputWindow
    outputWindow = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object

    Dim currentStackFrame As EnvDTE.StackFrame
    currentStackFrame = DTE.Debugger.CurrentStackFrame

    outputWindow.ActivePane.OutputString("*Dumping Local Variables*" + vbCrLf)

    For Each exp As EnvDTE.Expression In currentStackFrame.Locals
        If (exp.Name = "this") Then ' Here, I am only searching in a variable named "this"
            outputWindow.ActivePane.OutputString("- " + exp.Name + ": " + exp.Value.ToString() + vbCrLf)

            helperDisplay(exp.DataMembers, outputWindow, 1)
        End If

    Next
End Sub

Private Sub helperDisplay(ByRef exps As EnvDTE.Expressions, ByRef OutputWindow As EnvDTE.OutputWindow, ByVal n As Integer)

    For Each ex In exps
        helperTabs(n, OutputWindow)

        If (ex.Name = "Non-Public members" Or ex.Name = "Static members") Then
            OutputWindow.ActivePane.OutputString("~ " + ex.Name + ": There are " + ex.DataMembers.Count.ToString + vbCrLf)
        Else
            OutputWindow.ActivePane.OutputString("~ " + ex.Name + ": " + ex.Value.ToString() + vbCrLf)
            If (ex.DataMembers.Count <> 0) Then
                helperDisplay(ex.DataMembers, OutputWindow, n + 1)
            End If
        End If
    Next

End Sub

Private Sub helperTabs(ByVal n As Integer, ByRef OutputWindow As EnvDTE.OutputWindow)
    For i = 1 To n Step 1
        OutputWindow.ActivePane.OutputString(vbTab)
    Next
End Sub

This code is giving me exceptions for some DevExpress controls I tested it on. I think it's because some values can't be accessed. I have never coded in VB before so there are probably bugs in there...Play with it and see if it can do what you want (ie output only the properties that match what you need). The good part is, you can change the Macro code AS you are debugging, so no need to stop your project while you edit the Macro!

To use the Macro, you need to put a breakpoint somewhere in your code that will be hit, and will be able to access the object you want. -> Right click the breakpoint and go to When Hit... Check off Run a macro: and select your Macro from the dropdown.


When you are in debug mode, go up to Debug on the menu bar, and go to the drop down menu of windows. There, you will see locals. This will tell you what values your variables are at your current breakpoints. You can also dive deep down into all of your objects as well through a tree menu. To look for a particular object, you can watch, and then input the variable you want to watch there. Make sure you have the breakpoints in the right place. Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜