加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用断点从函数返回

发布时间:2020-12-15 18:33:33 所属栏目:百科 来源:网络整理
导读:是否可以使用断点/跟踪点自动从函数返回? 每次遇到断点时,我都不想拖动执行点或用CTRL SHIFT F10设置它. 我尝试“打
是否可以使用断点/跟踪点自动从函数返回?
每次遇到断点时,我都不想拖动执行点或用CTRL SHIFT F10设置它.
我尝试“打印”以下“消息”时命中,但执行继续没有改变.
{return;}
{return null;}

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

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

如果您不知道“打印消息”的含义,您可能需要阅读有关Tracepoints的this AltDevBlogADay post.很好.

解决方法

好吧,经过一些挖掘,你可以做到这一点 – 但它并不适用于所有情况.

请注意,这使用宏并且不能保证与内联委托一起使用;或者使用实际需要返回某些东西的方法.当断点被击中时,它会自动执行@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

有了这个,现在你可以设置你的断点 – 右键单击??它并点击When hit …菜单选项(这只适用于我相信的VS2010). ScottGu在this博文中描述了这一点.

从对话框中,找到刚刚粘贴的ExitStack宏.

运行附带调试器的代码,当命中断点时,应跳过函数代码的其余部分.这应该遵守其他调试器技巧 – 如条件等.

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

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

同样,如果断点位于try / catch块中,那么它将无法工作 – 因为必须先退出try / catch,然后才能将下一个语句设置到它之外的某个位置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读