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.
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Storm
  • 4,307
  • 11
  • 40
  • 57
  • 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? – Jon Cram Aug 10 '09 at 12:08
  • Duplicate: http://stackoverflow.com/questions/615940/c-how-to-retrieve-the-calling-method-from-within-a-method – Fredrik Mörk Aug 10 '09 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. – Greg D Aug 10 '09 at 12:26
  • The debugger knows how to do this - why does anyone else need to know how? – John Saunders Aug 11 '09 at 01:22

5 Answers5

10

You can try:

using System.Diagnostics;

StackTrace stackTrace = new StackTrace();
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • Hey weichsel this one is good..!! +1 from my side..!! Although this thread is a bit old but definitely useful..!! :) – samar Dec 16 '10 at 12:24
  • 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. – Tomas Mar 04 '13 at 21:26
1

I think you are looking for:

using System.Diagnostics;

StackTrace stackTrace = new StackTrace();
stackTrace.GetFrame(1).GetMethod().Name;
James
  • 80,725
  • 18
  • 167
  • 237
  • -1 Duplicate of higher voted older item (and also -1d the question on the same basis) – Ruben Bartelink Oct 15 '10 at 14:43
  • @Ruben - Never realised someone posting an answer faster than you deserved a downvote! – James Oct 15 '10 at 14:48
  • 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). – Ruben Bartelink Oct 15 '10 at 14:53
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());
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shane T
  • 212
  • 1
  • 5
  • Whau, that is probably the most interesting way I have ever seen anyone write to the console :-) – Jørn Schou-Rode Aug 10 '09 at 12:14
  • That looks like complicator code... http://thedailywtf.com/Articles/The_Complicator_0x27_s_Gloves.aspx – Greg D Aug 10 '09 at 12:22
  • 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 :) – Shane T Aug 10 '09 at 12:45
0

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

SimSimY
  • 3,616
  • 2
  • 30
  • 35
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"?.

Community
  • 1
  • 1
ALOR
  • 545
  • 1
  • 9
  • 20
  • 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. :) – Storm Aug 11 '09 at 11:35
  • I recently asked same question ;) There are lot of problems with StackFrame - if calling method gets inlined by JIT, you will get wrong results; – ALOR Aug 11 '09 at 15:33