Can we get the method name that we inside it.

  • Thread starter Thread starter Umut Tezduyar
  • Start date Start date
U

Umut Tezduyar

Inside a method, can we get the name of the method.

Explanation

public void MehtodName ()
{
Console.WriteLine (<The name of the method i am in; MethodName>);
}

Output:
MethodName

I hope i am clear enough to explain my issue.
 
You are wonderfull, you are perfect, you are #1
Thanks for your help,

For explanation, frame.GetMethod().Name returns the name of the method.
 
Hello,

You can use the StackFrame and StackTrace classes like this:

public void MehtodName ()
{
// initializes a new instance from the caller's frame
StackTrace trace = new StackTrace();

// gets the stack frame of the current method
StackFrame frame = trace.GetFrame(0);

// writes method signature
Console.WriteLine (frame.GetMethod());
}

GetMethod() will return the signature, so you will have to do some string
manipulation if you only want to have the name.

Regards,

Gabriele
 
Back
Top