How to get the name of current function?
48,713
Solution 1
Try this:
System.Reflection.MethodBase.GetCurrentMethod().Name
Solution 2
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 beware, if the method is inlined you get the parent method name.
Author by
Jack
Computer lover. :-) -- I'm not a native speaker of the english language. So, if you find any mistake what I have written, you are free to fix for me or tell me on. :)
Updated on September 11, 2020Comments
-
Jack over 3 years
Possible Duplicate:
Can you use reflection to find the name of the currently executing method?
C# how to get the name of the current method from codeFor example:
void foo() { Console.Write(__MYNAME__); }
print:
foo
it's possible do it in C#?
-
bohdan_trotsenko about 8 yearsFor people using .Net 4.5, there's CallerMemberNameAttribute