Top community members
All Wiki Articles Create Wiki Article

Many years me and my friends we were volunteers on my university doing programming classes for young people.

I got main conclusion: Young people are awesome, especially students, because they always bring new and fresh ideas :)
* I loved to do it but my time is limited

  I decided to create something that will help millions of students

Now I am here

co-founder

C# / .NET - get current method name

0 contributions
15 points

In this article, we're going to have a look at how to get currently called method name in C# / .NET.

Quick solution:

string methodName = nameof(this.SomeMethodHere);   //  C# 6 or later

// or

MethodBase method = MethodBase.GetCurrentMethod(); //  call it inside your method
string methodName = method.Name;

There are two approaches how to do it:

  • with built-in nameof operator - introduced in¬†C# 6,
  • with Reflection API.

1. nameof operator example

This approach do not use direcly using statement, reflection namespaces and classes. 

Note: the nameof operator is available in C# 6 and later.

using System;

public static class Program
{
	private static void DoMethod()
	{
		string name = nameof(DoMethod);

		Console.WriteLine("Currently called method name is " + name);
	}

	public static void Main(string[] args)
	{
		DoMethod();
	}
}

Output:

Currently called method name is DoMethod.

2. MethodBase.GetCurrentMethod method example

This solution was introduced in earlier version of .NET - can be used without warring it is not supported.

using System;
using System.Diagnostics;
using System.Reflection;

public static class Program
{
	private static void DoMethod()
	{
		MethodBase method = MethodBase.GetCurrentMethod();

		Console.WriteLine("Currently called method name is " + method.Name);
	}

	public static void Main(string[] args)
	{
		DoMethod();
	}
}

Output:

Currently called method name is DoMethod.

3. StackTrace class example

It is additional alternative way for previous examples.

using System;
using System.Diagnostics;
using System.Reflection;

public static class Program
{
	private static void DoMethod()
	{
		StackTrace trace = new StackTrace();

		StackFrame frame = trace.GetFrame(0);
		MethodBase method = frame.GetMethod();

		Console.WriteLine("Currently called method name is " + method.Name + ".");
	}

	public static void Main(string[] args)
	{
		DoMethod();
	}
}

Output:

Currently called method name is DoMethod.

References

  1. StackTrace Class - Microsoft docs
  2. StackFrame.GetMethod Method - Microsoft docs
  3. nameof operator - Microsoft Docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey ūüĎč
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more