Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of current function? [duplicate]

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 code

For example:

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

print: foo

it's possible do it in C#?

like image 686
Jack Avatar asked Apr 12 '12 19:04

Jack


2 Answers

Try this:

System.Reflection.MethodBase.GetCurrentMethod().Name  
like image 88
Raphaël Althaus Avatar answered Sep 21 '22 15:09

Raphaël Althaus


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.

like image 21
Albin Sunnanbo Avatar answered Sep 22 '22 15:09

Albin Sunnanbo