0

How do I find the method name of the calling method from a method at runtime?

For example:

Class A
{
    M1()
    {
        B.M2();
    }
}

class B
{
    public static M2()
    {
        // I need some code here to find out the name of the method that
        // called this method, preferably the name of the declared type
        // of the calling method also.
    }
}
4
  • 6
    Thinking you need to achieve something like this is often a good sign of a flaw in the design of an application. It may be better to look back at the design and see if you can fix that first. But, as can be the case, this is not always strictly true. Why do you need to do this? Commented Aug 10, 2009 at 12:08
  • Duplicate: stackoverflow.com/questions/615940/… Commented Aug 10, 2009 at 12:08
  • @Jon: I've never done it myself, though I've come close a couple of times. It can be a useful technique when trying to make sense of a steaming pile that has just landed on your plate. Commented Aug 10, 2009 at 12:26
  • The debugger knows how to do this - why does anyone else need to know how? Commented Aug 11, 2009 at 1:22

5 Answers 5

10

You can try:

using System.Diagnostics;

StackTrace stackTrace = new StackTrace();
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
Sign up to request clarification or add additional context in comments.

2 Comments

Hey weichsel this one is good..!! +1 from my side..!! Although this thread is a bit old but definitely useful..!! :)
Be aware that this probably won't give the same results if you compile in Release - as the JIT will inline/tail-call your methods if it can.
1

I think you are looking for:

using System.Diagnostics;

StackTrace stackTrace = new StackTrace();
stackTrace.GetFrame(1).GetMethod().Name;

3 Comments

-1 Duplicate of higher voted older item (and also -1d the question on the same basis)
@Ruben - Never realised someone posting an answer faster than you deserved a downvote!
I understand, and this isnt the first time people have rejected my analysis. If the answer didnt have an upvote, I wouldnt downvote. I know that's inconsistent under reducto ad absurdum. (The other question has better answers - I was looking for an answer that covered the NoInlining point and syntax). (I personally either delete dups I create or edit them to become different and/or better than the other answers - if someone else covered the same ground before me, I'm happy to have it pointed out and/or get downvoted).
0

Check the System.Diagnostics.Trace class, but as far as I know - there is performance price when using it

Comments

0

Better not to use StackFrame, because there are some .NET security issues. If the code is not fully trusted, the expression "new StackFrame()" will throw a security exception.

To get the current method use:

MethodBase.GetCurrentMethod().Name

About getting calling the method, see Stack Overflow question Object creation, how to resolve "class-owner"?.

2 Comments

Thank ALor, but I guess you missed the point. I don't want the current method name, i need the method name of the method that called the current method. :)
I recently asked same question ;) There are lot of problems with StackFrame - if calling method gets inlined by JIT, you will get wrong results;
0

You can do this by displaying the call stack, as illustrated in the code below. This will find the entire call stack, not just the calling method though.

void displaycallstack() {
    byte[] b;
    StackFrame sf;
    MemoryStream ms = new MemoryStream();
    String s = Process.GetCurrentProcess().ProcessName;
    Console.Out.WriteLine(s + " Call Stack");
    StackTrace st = new StackTrace();
    for (int a = 0;a < st.FrameCount; a++) {
        sf = st.GetFrame(a);
        s = sf.ToString();
        b = Encoding.ASCII.GetBytes(s);
        ms.Write(b,0,b.Length); 
    }
    ms.WriteTo(System.Console.OpenStandardOutput());
}

3 Comments

Whau, that is probably the most interesting way I have ever seen anyone write to the console :-)
Jorn, after looking at the code a little more closely.. I'd have to agree. It was just taken from an example on how to display the stack trace. I'll have to find better examples next time :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.