Using StackFrame and MethodBase to Determine Message Callers

Introduction

Welcome to my article. Today, I will demonstrate how to use the System.Reflection class’ methods to get various information on which methods called certain methods.

In a large program, it sometimes can get cumbersome to know from where a certain message came from. By using classes such as StackTrace and MethodBase, you can easily find out the message calling information.

Namespaces

Namespaces organize the objects defined in an assembly. Assemblies can contain multiple namespaces, which, in turn, can contain other namespaces. Namespaces prevent ambiguity and simplify references when using large groups of objects such as class libraries, as quoted from MSDN.

System.Reflection Namespace

The System.Reflection namespace contains types that retrieve information about modules, members, assemblies, parameters, and other entities in managed code by examining their metadata. These types also can be used to manipulate instances of loaded types. Here is more information on the System.Reflection Namespace.

StackTrace

StackTrace represents a stack trace, which is an ordered collection of one or more stack frames. Here is more information on StackTraces. All messages get loaded onto a stack, and you can use StackTrace to trace where a certain method is in the current message stack.

StackFrame

The StackFrame class provides information about a StackFrame, which represents a function call on the call stack for the current thread. Read here for more information:

https://msdn.microsoft.com/en-us/library/system.diagnostics.stackframe%28v=vs.110%29.aspx

The MethodBase class provides information about methods and constructors, and where they come from. Here is more information:

https://msdn.microsoft.com/en-us/library/system.reflection.methodbase%28v=vs.110%29.aspx

Our Project

Start Visual Studio and create a new Visual Basic Windows Forms project. Add two buttons to the form’s design.

Code

Add the following code to get the current executing method’s name:

   Private Sub GetCurrentMethodName()

      Dim sfFrame As StackFrame = New StackFrame()
      Dim mbMethod As MethodBase = sfFrame.GetMethod()
      MessageBox.Show(mbMethod.Name)

   End Sub

Here, I created a new StackFrame object, from which I created a MethodBase object. Then, I simply show the current method’s name inside a MessageBox.

Call this function with the following piece of code:

   Private Sub Button1_Click(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles Button1.Click
      GetCurrentMethodName()

   End Sub

Add the following code to find out which method called a certain method:

   Private Sub WhoCalledTheMethod()

      Dim stTrace As StackTrace = New StackTrace()
      Dim sfFrame As StackFrame = stTrace.GetFrame(1)
      Dim mbMethod As MethodBase = sfFrame.GetMethod()

      MessageBox.Show("I was called by: " & mbMethod.Name)

   End Sub

In the preceding code, I created a StackTrace object and then a StackFrame and a MethodBase object to determine the method caller. Lastly, I simply display the result inside a MessageBox.

Conclusion

Although this article was a bit short, it was quite powerful and a good way to find out calling information from methods via the StackTrace class. Until next time, cheers!

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read