views:

530

answers:

5

I know you can do

this.GetType().FullName

Edit courtesy of @Pasi Savolainen

To get

My.Current.Class

But what can I call to get

My.Current.Class.CurrentMethod

+2  A: 

Check this out: http://www.codeproject.com/KB/dotnet/MethodName.aspx

Brian
+17  A: 
StackTrace st = new StackTrace ();
StackFrame sf = st.GetFrame (0);

MethodBase currentMethodName = sf.GetMethod ();

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

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

    return sf.GetMethod ();
}

Updated with credits to @stusmith.

Developer Art
Bear in mind the JIT is free to inline methods, which might throw this off. To force a method to not be inlined, add the [MethodImpl(MethodImplOptions.NoInlining)] attribute.
stusmith
@stusmith: Thanks! Will remember it!
Developer Art
@stusmith Thanks
Nick Allen - Tungle139
Note that adding `MethodImpl(MethodImplOptions.NoInlining)` to `GetCurrentMethod` only stops that method from being inlined into its caller. It doesn't prevent the calling method itself from being inlined into its own caller etc, etc.
LukeH
@Luke: That is of course possible and complicates things.
Developer Art
Sorry yes I wasn't clear - you need to add the attribute to the method for which you want to know the name of.
stusmith
@stusmith: The GetCurrentMethod would need the attribute as well, right? It could also become inlined why not.
Developer Art
+12  A: 

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

Jamie Ide
+4  A: 

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
João Angelo
Do you have a reference explaining how `GetCurrentMethod` inhibits the JIT from inlining the calling method?
LukeH
@Luke, I updated my answer with the reason why I believe the method will inhibit the inlining.
João Angelo
Thanks, that's very interesting, although it's not entirely clear to me whether it prevents inlining of the method that declares the local `StackCrawlMark` or the caller of that method.
LukeH
@Luke, yes it can cast a certain doubt, but since `GetCurrentMethod` itself is decorated with `[MethodImpl(MethodImplOptions.NoInlining)]` I believe it's the caller of the method containing the enumeration.
João Angelo
+3  A: 

Reflection has a knack for hiding the forest for the trees. You never have a problem getting the current method name accurately and quickly:

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

Albeit that a refactoring tool 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;
} 
Hans Passant
I've done this before. Can be a sod to maintain if you do this application-wide, but otherwise: Simple beats clever any day!
Daren Thomas
True words, it is about 10,000 times faster, give or take a factor of 10.
Hans Passant