使用断点从函数返回

Pro*_*one 6 c# debugging breakpoints visual-studio

是否可以使用断点/跟踪点自动从函数返回?
每次遇到断点时,我都不想拖动执行点或用CTRL + SHIFT + F10设置它.
我尝试"打印"以下"消息"时命中,但执行继续没有改变.

{return;}
{return null;}
Run Code Online (Sandbox Code Playgroud)

请注意,我需要从函数返回而不实际更改代码.

为了阐明Tracepoint是什么:"跟踪点是一个断点,其中包含与之关联的自定义操作.当命中跟踪点时,调试器会执行指定的跟踪点操作,而不是破坏程序执行." 来自MSDN.

如果您不知道"打印消息"的含义,您可能需要阅读这篇关于Tracepoints的AltDevBlogADay帖子.很好.

jue*_*n d 7

在Visual Studio中,您只需将指示调试时当前代码行的箭头拖动到函数末尾即可.

  • +1,或右键单击函数中的return语句,或大括号,然后选择'set next statement'.虽然值得指出的是,它仅在将代码指向当前堆栈中的其他位置时才起作用; 我认为try/catch块不能生效. (4认同)

And*_*tan 4

好吧,经过一番研究后你可以做到这一点 - 但它并不适用于所有情况。

请注意,这使用了宏,并且不能保证与内联委托一起使用;或者使用实际需要返回某些内容的方法。当遇到断点时,它会自动执行@juergen d 和@Erno 描述的过程;使用非常简单的启发式方法来查找当前函数的结尾位置。

首先需要将此宏添加到您的宏环境中(在 VS 中使用 ALT+F11 打开)。这段代码可能并不那么好,因为我刚刚匆匆忙忙地把它写出来了:)

Sub ExitStack()
    'get the last-hit breakpoint
    Dim breakPoint As EnvDTE.Breakpoint
    breakPoint = DTE.Debugger.BreakpointLastHit()
    'if the currently open file is the same as where the breakpoint is set
    '(could search and open it, but the debugger *should* already have done that)
    If (DTE.ActiveDocument.FullName = breakPoint.File) Then

        Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
        Dim editPoint As EnvDTE.EditPoint
        'move the cursor to where the breakpoint is actually defined
        selection.MoveToLineAndOffset(breakPoint.FileLine, breakPoint.FileColumn)

        Dim codeElement As EnvDTE.CodeElement
        codeElement = DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElementFromPoint(selection.ActivePoint, vsCMElement.vsCMElementFunction)
        'if a function is found, move the cursor to the last character of it
        If Not (codeElement Is Nothing) Then
            Dim lastLine As EnvDTE.TextPoint

            lastLine = codeElement.GetEndPoint()
            selection.MoveToPoint(lastLine)
            selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
            'execute the SetNextStatement command.  
            'Has to be done via ExecuteCommand
            DTE.ExecuteCommand("Debug.SetNextStatement")
        End If
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

完成后,现在您可以设置断点 - 右键单击​​它并点击When hit...菜单选项(我相信这只适用于 VS2010)。ScottGu 在这篇博文中描述了这一点。

从对话框中找到ExitStack您刚刚粘贴的宏。

使用附加的调试器运行代码,当遇到断点时,应跳过函数代码的其余部分。这应该遵循其他调试器技巧 - 例如条件等。

注意-我用这个SO来解决我遇到的问题;最初我是直接调用调试器的 SetNextStatement 方法,但它不起作用

我不知道应该返回的方法将如何表现 - 理论上它们应该返回当时本地的返回值,但在某些情况下,事实是这根本行不通!

同样,如果断点位于 try/catch 块中,那么它将不起作用 - 因为必须退出 try/catch 才能将下一条语句设置到其外部的某个位置。