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

c# – 当我的类库函数被调用时,有没有办法自动进入调试器?

发布时间:2020-12-15 04:20:34 所属栏目:百科 来源:网络整理
导读:我有一个托管类库(比如mylib.dll)和一个使用mylib.dll的第三方托管应用程序(说app.exe).我有mylib.dll的代码,但不是app.exe.所以目前我做的是我建立mylib.dll,将其复制到app.exe的目录下,启动app.exe并附加到该进程.这样,如果我在代码mylib.dll中插入断点,我
我有一个托管类库(比如mylib.dll)和一个使用mylib.dll的第三方托管应用程序(说app.exe).我有mylib.dll的代码,但不是app.exe.所以目前我做的是我建立mylib.dll,将其复制到app.exe的目录下,启动app.exe并附加到该进程.这样,如果我在代码mylib.dll中插入断点,我看到他们被击中.
但是,无论何时任何外部应用程序调用其中一个暴露的方法,是否自动中断mylib.dll的代码?即.只用于dll的入口点.

谢谢,
米沙勒

解决方法

在项目 – >属性 – >调试 – >开始动作你应该指定选项启动外部程序并输入你的app.exe的路径.这应该启动app.exe附加的调试器.

参见How to: Change the Start Action for Application Debugging

更新:Visual Studio中的断点或者绑定到特定位置(即源文件中的特定代码行)或函数名称.因此,您基本上有两个选项可以在您的程序集中调用一个函数:在所有函数声明或所有函数名上放置断点(Debug – > New Breakpoint – > Break on Function Name).不幸的是,后一个选项需要完整的函数名称,不允许使用通配符.

您可能会考虑的另一个选择是将Debug.Assert(false)放在所有库函数的开头.

另一个选择是使用Visual Studio宏.下面的宏将遍历您的代码DOM,并向所有公共方法和属性添加断点:

Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Windows.Forms

Public Module Breakpoints

    Sub AddBreakpointsToAllFunctionsAndProperties()
        Try
            If DTE.ActiveSolutionProjects.Length <> 1 Then
                MsgBox("Select one project within the Solution Explorer,then re-run this macro.")
                Exit Sub
            End If

            AddBreakpointsToProject(DTE.ActiveSolutionProjects(0))
        Catch ex As System.Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub AddBreakpointsToProject(ByVal proj As Project)
        For i As Integer = 1 To proj.ProjectItems.Count
            If Not proj.ProjectItems.Item(i).FileCodeModel Is Nothing Then
                AddBreakpointsToProjectItems(proj.ProjectItems.Item(i).FileCodeModel.CodeElements)
            End If
        Next
    End Sub


    Private Sub AddBreakpointsToProjectItems(ByVal colCodeElements As CodeElements)
        Dim objCodeElement As EnvDTE.CodeElement

        If Not (colCodeElements Is Nothing) Then
            For Each objCodeElement In colCodeElements
                AddBreakpointsToProjectItem(objCodeElement)
            Next
        End If
    End Sub

    Private Sub AddBreakpointsToProjectItem(ByVal objCodeElement As CodeElement)

        Dim objCodeNamespace As EnvDTE.CodeNamespace
        Dim objCodeType As EnvDTE.CodeType
        Dim objCodeFunction As EnvDTE.CodeFunction
        Dim objCodeProperty As EnvDTE.CodeProperty

        Try
            'MessageBox.Show(objCodeElement.FullName & " (Kind: " & objCodeElement.Kind.ToString & ")")

            If objCodeElement.Kind = vsCMElement.vsCMElementFunction Then
                objCodeFunction = DirectCast(objCodeElement,EnvDTE.CodeFunction)
                If objCodeFunction.Access = vsCMAccess.vsCMAccessPublic Then
                    DTE.Debugger.Breakpoints.Add(objCodeElement.FullName)
                End If
            ElseIf objCodeElement.Kind = vsCMElement.vsCMElementProperty Then
                objCodeProperty = DirectCast(objCodeElement,EnvDTE.CodeProperty)
                DTE.Debugger.Breakpoints.Add(objCodeElement.FullName)
            End If
        Catch ex As System.Exception
            ' Ignore
        End Try

        If TypeOf objCodeElement Is EnvDTE.CodeNamespace Then
            objCodeNamespace = CType(objCodeElement,EnvDTE.CodeNamespace)
            AddBreakpointsToProjectItems(objCodeNamespace.Members)
        ElseIf TypeOf objCodeElement Is EnvDTE.CodeType Then
            objCodeType = CType(objCodeElement,EnvDTE.CodeType)
            AddBreakpointsToProjectItems(objCodeType.Members)
        ElseIf TypeOf objCodeElement Is EnvDTE.CodeFunction Then
            objCodeFunction = DirectCast(objCodeElement,EnvDTE.CodeFunction)
            AddBreakpointsToProjectItems(CType(objCodeElement,CodeFunction).Parameters)
        End If
    End Sub

End Module

(编辑:李大同)

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

    推荐文章
      热点阅读