How to get the name of current function?

C#.Netsystem.reflection

C# Problem Overview


> 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#?

C# Solutions


Solution 1 - C#

Try this:

System.Reflection.MethodBase.GetCurrentMethod().Name 

Solution 2 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJackView Question on Stackoverflow
Solution 1 - C#Raphaël AlthausView Answer on Stackoverflow
Solution 2 - C#Albin SunnanboView Answer on Stackoverflow