How to get the name of the current method from code

C#Introspection

C# Problem Overview


I know you can do

this.GetType().FullName

To get

My.Current.Class

But what can I call to get

My.Current.Class.CurrentMethod

C# Solutions


Solution 1 - C#

Call System.Reflection.MethodBase.GetCurrentMethod().Name from within the method.

Solution 2 - C#

using System.Diagnostics;
...

var st = new StackTrace();
var sf = st.GetFrame(0);

var currentMethodName = sf.GetMethod();

Or, if you'd like to have a helper method:

[MethodImpl(MethodImplOptions.NoInlining)]
public string GetCurrentMethod()
{
    var st = new StackTrace();
    var sf = st.GetFrame(1);

    return sf.GetMethod().Name;
}

Updated with credits to @stusmith.

Solution 3 - C#

Since C# version 6 you can simply call:

string currentMethodName = nameof(MyMethod);

In C# version 5 and .NET 4.5 you can use the [CallerMemberName] attribute to have the compiler auto-generate the name of the calling method in a string argument. Other useful attributes are [CallerFilePath] to have the compiler generate the source code file path and [CallerLineNumber] to get the line number in the source code file for the statement that made the call.


Before that there were still some more convoluted ways of getting the method name, but much simpler:

void MyMethod() {
  string currentMethodName = "MyMethod";
  //etc...
}

Albeit that a refactoring probably won't fix it automatically.

If you completely don't care about the (considerable) cost of using Reflection then this helper method should be useful:

using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Reflection;
//...

[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetMyMethodName() {
  var st = new StackTrace(new StackFrame(1));
  return st.GetFrame(0).GetMethod().Name;
} 

Solution 4 - C#

I think the best way to get the full name is:

 this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name;

or try this

string method = string.Format("{0}.{1}", MethodBase.GetCurrentMethod().DeclaringType.FullName, MethodBase.GetCurrentMethod().Name);   

Solution 5 - C#

Does this not work?

System.Reflection.MethodBase.GetCurrentMethod()

> Returns a MethodBase object representing the currently executing method.

> Namespace: System.Reflection

> Assembly: mscorlib (in mscorlib.dll)

http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod.aspx

Solution 6 - C#

You can also use MethodBase.GetCurrentMethod() which will inhibit the JIT compiler from inlining the method where it's used.


Update:

This method contains a special enumeration StackCrawlMark that from my understanding will specify to the JIT compiler that the current method should not be inlined.

This is my interpretation of the comment associated to that enumeration present in SSCLI. The comment follows:

// declaring a local var of this enum type and passing it by ref into a function 
// that needs to do a stack crawl will both prevent inlining of the calle and 
// pass an ESP point to stack crawl to
// 
// Declaring these in EH clauses is illegal; 
// they must declared in the main method body

Solution 7 - C#

Well System.Reflection.MethodBase.GetCurrentMethod().Name is not a very good choice 'cause it will just display the method name without additional information.

Like for string MyMethod(string str) the above property will return just MyMethod which is hardly adequate.

It is better to use System.Reflection.MethodBase.GetCurrentMethod().ToString() which will return the entire method signature...

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
QuestionNick AllenView Question on Stackoverflow
Solution 1 - C#Jamie IdeView Answer on Stackoverflow
Solution 2 - C#user151323View Answer on Stackoverflow
Solution 3 - C#Hans PassantView Answer on Stackoverflow
Solution 4 - C#ZakariaView Answer on Stackoverflow
Solution 5 - C#OrwellophileView Answer on Stackoverflow
Solution 6 - C#João AngeloView Answer on Stackoverflow
Solution 7 - C#ArefinView Answer on Stackoverflow