How to write function's name as a string in C#

Hakan
Hakan used Ask the Experts™
on
I've a case like below;

public static void POP_YTL_GLOBAL() {}

is it possible to get it's name inside it like below;

public static void POP_YTL_GLOBAL()
{
string nameOfFunction = // How to get POP_YTL_GLOBAL automatically in here.
}

Any help would be grateful.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You can give this a go:

public static void POP_YTL_GLOBAL()
{
    var method = MethodBase.GetCurrentMethod();
    string nameOfFunction = method.Name;
}

Open in new window

NorieAnalyst Assistant
Commented:
Hakan

You could use System.Reflection.
  public static void POP_YTL_GLOBAL()
  {
  string nameOfFunction = System.Reflection.MethodBase.GetCurrentMethod().ToString();
  Console.WriteLine(nameOfFunction);
  }

Open in new window

Author

Commented:
Thank you it worked :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial