How to get the name of the current function?

Possible duplicate:
Can you use reflection to find the name of the current executable method?
C # how to get the name of the current method from code

For example:

void foo() { Console.Write(__MYNAME__); } 

print: foo

is it possible to do this in c #?

+42
c # system.reflection
Apr 12 2018-12-12T00:
source share
2 answers

Try the following:

 System.Reflection.MethodBase.GetCurrentMethod().Name 
+99
Apr 12 2018-12-12T00:
source share

You can check the stack trace

 using System.Diagnostics; // get call stack StackTrace stackTrace = new StackTrace(); // get calling method name Console.WriteLine(stackTrace.GetFrame(0).GetMethod().Name); 

But be careful, if the method is built-in, you get the name of the parent method.

+14
Apr 12 2018-12-12T00:
source share



All Articles