StackTrace stackTrace = new StackTrace(); foreach (StackFrame frame in stackTrace.GetFrames()) { Console.WriteLine(frame.GetMethod().Name); }
public void DoSomething() { StackTrace stackTrace = new StackTrace(); StackFrame caller = stackTrace.GetFrame(1); if (caller != null) { MethodBase method = caller.GetMethod(); Console.WriteLine("Called by {0} in {1} at line {2}", method.Name, method.DeclaringType.FullName, caller.GetFileLineNumber()); } }This example retrieves the method that called the DoSomething() method using the StackTrace object and prints out information about the caller, including the method name, type name, and line number. Package Library: System.Diagnostics.StackTrace can be found in the System.Diagnostics namespace of the .NET Framework Class Library.