VB.NET•Extract Method Information From Stack Frames

Listing 2. The stack frame contains information about a location in your code that's currently in the execution stack. The MethodWithParams method returns the method name, parameter types, and values, as passed from the calling code.

Private Shared Sub SetStack( _
	ByVal stackFrame As StackFrame, _
	ByVal params() As Object, _
	ByRef nspace As String, _
	ByRef className As String, _
	ByRef methodName As String, _
	ByRef row As Int32, _
	ByRef col As Int32)
	Dim method As Reflection.MethodBase = _
		stackFrame.GetMethod()
	nspace = method.ReflectedType.Namespace
	className = method.ReflectedType.Name
	methodName = MethodWithParams(method, params)
	row = stackFrame.GetFileLineNumber
	col = stackFrame.GetFileColumnNumber
End Sub

Private Shared Function MethodWithParams( _
	ByVal method As Reflection.MethodBase, _
	ByVal params() As Object) As String
	Dim ret As String = method.Name & "("
	Dim methodparams As Reflection.ParameterInfo()
	methodparams = method.GetParameters()
	If Tools.SkipArguments _
		Or methodparams.Length = 0 Then
		Return ret & ")"
	Else
		If params Is Nothing Then
			For i As Int32 = 0 To _
					methodparams.GetUpperBound(0)
				ret &= methodparams(i). _
					ParameterType.Name & ", "
			Next
			Return ret.Substring _
				(0, ret.Length - 1) & ")"
		ElseIf methodparams.Length <> _
				params.Length Then
			Return ret & _
				"trace param info is inconsistent )"
		Else
			For i As Int32 = 0 To _
				methodparams.GetUpperBound(0)
				If methodparams(i).ParameterType _
					Is GetType(System.String) Then
				ret &= methodparams(i). _
					ParameterType.Name & " " & _
					methodparams(i).Name & "=" & _
					"""" & params(i).ToString & _
					""", "
				Else
					ret &= methodparams(i). _
						ParameterType.Name & " " & _
						methodparams(i).Name & "=" _
						& params(i).ToString & ", "
				End If
			Next
			Return ret.Substring _
				(0, ret.Length - 1) & ")"
		End If
	End If
End Function