Alex Rivera | Logout

How to get the name of the current method from code

Asked 2010-04-16T11:24:09.027
524

I know you can do

this.GetType().FullName

To get

My.Current.Class

But what can I call to get

My.Current.Class.CurrentMethod
Edit
Report

2 Answers

450
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 static string GetCurrentMethod()
{
    var st = new StackTrace();
    var sf = st.GetFrame(1);

    return sf.GetMethod().Name;
}

Updated with credits to @stusmith.

answered 2010-04-16T11:26:01.313
15

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

answered 2012-06-21T09:05:11.883

Your Answer