How to get the source file name and the line number of a type member?

后端 未结 3 1316
日久生厌
日久生厌 2020-12-03 13:50

Considering that the debug data file is available (PDB) and by using either System.Reflection or another similar framework such as Mono.Cecil

3条回答
  •  [愿得一人]
    2020-12-03 14:44

    you might find some help with these links:

    Getting file and line numbers without deploying the PDB files also found this following post

    "Hi Mark,

    The following will give you the line number of your code (in the source file):

    Dim CurrentStack As System.Diagnostics.StackTrace
    MsgBox (CurrentStack.GetFrame(0).GetFileLineNumber)
    

    In case you're interested, you can find out about the routine that you're in, as well as all its callers.

    Public Function MeAndMyCaller As String
        Dim CurrentStack As New System.Diagnostics.StackTrace
        Dim Myself As String = CurrentStack.GetFrame(0).GetMethod.Name
        Dim MyCaller As String = CurrentStack.GetFrame(1).GetMethod.Name
        Return "In " & Myself & vbCrLf & "Called by " & MyCaller
    End Function
    

    This can be very handy if you want a generalised error routine because it can get the name of the caller (which would be where the error occurred).

    Regards, Fergus MVP [Windows Start button, Shutdown dialogue] "

提交回复
热议问题