How to find out the immediate calling method
Use the
StackFrame
and
StackTrace
classes.
For example:
public static void SourceMethod()
{
TargetMethod();
}
public static void TargetMethod()
{
StackTrace st = new StackTrace();
// This will output 'SourceMethod'
Console.WriteLine(st.GetFrame(1).GetMethod().Name);
}
Note:
Code optimizations might give you different results. Some methods may
even be completely optimized out. But this code should work fine without
any optimizations.