Not logged in - Login

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 spot 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;
}