Find the MethodBase value of the calling method
Technique to find the MethodBase object of the method that has called the current method.
public static void Foo()
{
// declare our MethodBase variable.
System.Reflection.MethodBase method = null;
// get the stack frame 1 stop from our current position,
// this will be the method that called the current method.
System.Diagnostics.StackFrame frame =
new System.Diagnostics.StackFrame(1);
// make sure we got back a valid StackFrame value
if (frame != null)
{
// get the method object of the StackFrame object.
method = frame.GetMethod();
}
else
{
// no previous StackFrame object found???
// This should never happen. Get the current method so we'll have
// a valid value.
method = System.Reflection.MethodInfo.GetCurrentMethod();
}
// At this point, we have a valid value for the 'method' object.
// Do something with it. For example, get the name of the method.
string name = method.Name;
}
Possibly an easier way to do the same thing: Find the name of the current method using CallerMemberName attribute
Last modified by Mohit @ 4/16/2025 5:17:02 AM