C/C++ で言うところの、__FILE__ や __LINE__ を拾ってログ出力したい、という場合
次のような関数を作っておきます。
1 2 3 4 5 6 7 8 9 10 11 12 | Public Class DebugTrace Public Shared Sub Trace() Dim sf = New StackFrame(1, True ) Dim methodName As String = sf.GetMethod().ToString() Dim fileName As String = sf.GetFileName() Dim lineNumber As Integer = sf.GetFileLineNumber() MessageBox.Show( String .Format( _ "場所 {0} 場所 {1}:行 {2}" , methodName, fileName, lineNumber)) End Sub End Class |
で、使いたいときは、
1 2 3 | Private Sub Button1_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles Button1.Click DebugTrace.Trace() End Sub |
のように書けばOK.
StackFrameクラスでは、Exceptionのように呼び出し元を再帰的に呼べるはずですが、ひとまずこれで用は足りるからよいかな、と。