As methods are called, information about them are placed on the call stack. The call stack is invaluable in development work for debugging problems, but it can also be useful in other situations (e.g. when logging for an entire app or library is processed by a logging utility in one location).
Access the call stack via a new instance of the StackTrace class. The stack contains stack frames, these frames represent each method executing on the current thread; programmatically access a frame via a StackFrame instance. Using the StackFrame.GetMethod(), which returns a MethodBase object, you can now easily retrieve the detailed information you wanted. The sample code below demonstrates this.
// First create an instance of the call stack StackTrace callStack = new StackTrace(); // Next select the frame we want... // 0 : current frame for the current method // 1 : Frame that called the current method // 2 : Frame that called the frame that called the current method // 3 : ...you get the idea! StackFrame frame = callStack.GetFrame(1); // Using StackFrame.GetMethod(), which returns a // MethodBase object, we can obtain detailed // information about about a method MethodBase method = frame.GetMethod(); // Get the declaring type and method names string declaringType = method.DeclaringType.Name; string methodName = method.Name;