Identify the Caller of a Procedure
Problem
You want your class to determine some information about the calling code, probably for diagnostic purposes.
Solution
Use the System.Diagnostics.StackTrace class.
Discussion
You can’t retrieve information about the caller of a procedure through reflection. Reflection can only act on metadata stored in the assembly, whereas the caller of a procedure is determined at runtime. However, .NET includes two useful diagnostic classes that fill this role: StackTrace and StackFrame.
The stack holds a record of every call that is open and not yet completed. As new calls are made, new methods are added to the top of the stack. For example, if method A calls method B, both method A and B will be on the stack (with method ...
Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.