Friday, January 22, 2010

Get the name of the method

Sometimes it is easy to print the methodname of the current method. This can be typed manually, but with use of Reflection this can be retrieved with the following simple command:

System.Reflection.MethodBase.GetCurrentMethod().Name

1 comment:

  1. That'll work, but you got to remember that the JIT could possibly do an inline when compiled in release mode, so if you *really* need to get the name of the current method, you should put a MethodImplAttribute on it telling it to disable inlining.

    [MethodImpl(MethodImplOptions. NoInlining)]
    public void Hello()
    {
    //Omitted
    }

    ReplyDelete