ExecutingMethodName
is intended to print the method name of the caller. For example:
static void Main(string[] args){Auxiliary.ExecutingMethodName();}
should print Main.
static void Foo(){Auxiliary.ExecutingMethodName();}
should print Foo.
static class Auxiliary
{
public static void ExecutingMethodName()
{
Console.WriteLine(new StackFrame(0).GetMethod().Name);
}
}
class Program
{
static void Main(string[] args)
{
Auxiliary.ExecutingMethodName();// should print Main
}
static void Foo()
{
Auxiliary.ExecutingMethodName();// should print Foo
}
}
The current implementation above always print ExecutingMethodName
that is not what I want. How to print the current executing method name via an auxiliary method?
Just change 0 to 1 in stackframe call in your method (StackFrame(0)
is your currrent position in call stack and you need to go one step back):
public static void ExecutingMethodName()
{
Console.WriteLine(new StackFrame(1).GetMethod().Name);
}