Can I find out the name of the method I am using?

Say I have some code like

namespace Portal
{
  public class Author
    {
        public Author() { }
        private void SomeMethod(){
          string myMethodName = ""; 
          //  myMethodName = "Portal.Author.SomeMethod()";
        }
    }
}

Can I find out the name of the method I am using? In my example I'ld like to programmatically set myMethodName to the name of the current method (ie in this case "Portal.Author.SomeMethod").

Thanks

Answers


MethodInfo.GetCurrentMethod().Name

System.Reflection.MethodBase.GetCurrentMethod().Name


MethodBase.GetCurrentMethod()


System.Diagnostics has the StackFrame/StackTrace which allows you to just that, including more. You can inspect the entire call stack like so:

StackFrame stackFrame = new StackFrame(1, true); //< skip first frame and capture src info
StackTrace stackTrace = new StackTrace(stackFrame);
MethodBase method = stackTrace.GetMethod();
string name = method.Name;

While @leppie put me on the right track, it only gave me the name of the method, but if you look at my question you will see I wanted to include namespace and class information...

so

myMethodName = System.Reflection.MethodBase.GetCurrentMethod().ReflectedType + 
                 "." + System.Reflection.MethodBase.GetCurrentMethod().Name;

Need Your Help

Android - Dialog Button onClick -> EditText Value

android dialog onclick android-edittext

I have a "Search" Button in my Options menu, which shows a Dialog with the Search Mask.

Selecting an item in comboBox by typing

c# winforms combobox

I've a combobox that has hundreds item in it. User must be able to type the text into the combobox . While the user is typing the text, the item that starting with the typed value must be selected or