23

Possible Duplicate:
How can I find the method that called the current method?

I need a way to know the name of calling methods in C#.

For instance:

private void doSomething()
{
// I need to know who is calling me? (method1 or method2).

// do something pursuant to who is calling you?
} 

private void method1()
{
 doSomething();
}

private void method2()
{
 doSomething();
}
Community
  • 1
  • 1
ecleel
  • 11,748
  • 15
  • 48
  • 48
  • 1
    Under what circumstances would you need to know that? – mezoid Dec 27 '08 at 10:11
  • 1
    This smells like bad programming practise, Please tell us why you want to do that? ;-) – Sander Versluys Dec 27 '08 at 10:25
  • 1
    Pass a parameter to the doSomething() function depending on which one calls it. – Schotime Dec 27 '08 at 11:45
  • 1
    Yes, this is usually not a good idea – configurator Dec 27 '08 at 16:23
  • I can't think of a situation where this was a good idea. – John Saunders Apr 21 '09 at 13:06
  • 3
    I can think of a situation where this is a good idea - if you have shared code responsible for some cross cutting concern, e.g. logging, you might want this information to specialize the output from the shared code - I use this practice in service call logging, a utility function sets up security and logging for the service call and uses the identity of the calling function for both logging and a check on security access rights – Mark Mullin Feb 23 '11 at 14:23
  • 1
    @Mark: as I say in my answer, the best practice would be to not have the code know about the callers, but rather to have the code know about the the different specialized scenarios - and have the caller say which scenario is desired. – John Saunders Nov 07 '11 at 23:51
  • Realize this is 3 yrs old, but I agree with Mark. Came across this question because of one such scenario. Have a Windows service that times out on first attempt to start 70%+ of the time. After getting "service failed to start in a timely manner...." I immediately click Start again and it always works. Trying to put logging in various places in numerous classes. Don't want to create an EventLog object in every class. Have a "central" one that accepts "message" to log and adds other info. Want calling code to be short as possible, i.e. should not need to identify itself. – Andrew Steitz Nov 11 '14 at 15:05

2 Answers2

61

from http://www.csharp-examples.net/reflection-calling-method-name/

using System.Diagnostics;

// get call stack
StackTrace stackTrace = new StackTrace();

// get calling method name
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
  • 7
    I think in release builds you can't depend on this since the jit compiler could optimize code and so the name could be different. – Stormenet Dec 27 '08 at 10:37
  • But How I can get preveous method call. changing GetFrame() give you all methods in calling stack. – ecleel Dec 27 '08 at 11:00
  • 5
    That's what the (1) is for. You must use [MethodImpl(MethodImplOptions.NoInlining)] to ensure your method doesn't get inlined. – Hans Passant Dec 27 '08 at 12:27
  • 5
    True, however extracting stack traces for any reason other than debugging or logging should be avoided. This can cause horrible performance. – Brian Rudolph Dec 27 '08 at 18:15
  • for me, i use this as a quick debug helper to know w/c methods called a particular method. – sjlewis Feb 08 '11 at 18:39
  • Some people say this is bad for performance. Is it true? – Louis Rhys Jun 19 '12 at 01:56
  • 1
    Performance is probably worse, but in my case (called max ~150 times in a row), I could not even measure the difference. BUT, I cannot underline enough what Stormenet/Hans Passant said about not depending on this in release builds. I spent a woeful 2 days of searching for a bug, then finding this problem. It's about as cloudy as it can get, because you can add nonsensical code that will prevent the inlining, and also your code will work in debug mode. – Mike Fuchs Jun 13 '13 at 15:50
7

You almost certainly don't want to do this. A caller should never know who is calling it. Instead, the difference between the two callers should be abstracted into a parameter, and passed into the method being called:

private void doSomething(bool doItThisWay)
{
    if (doItThisWay)
    {
        // Do it one way
    }
    else
    {
        // Do it the other way
    }
}

private void method1()
{
    doSomething(true);
}

private void method2()
{
    doSomething(false);
}

This way, if you add a method3, it can either doSomething one way or the other, and doSomething won't care.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 3
    You might want to do that only if you are outputting some debug information – Andrey Nov 02 '10 at 22:08
  • 2
    @Andrey: debugging and such is always a special case. If the OP was interested in debugging, he should probably have said so. Outside of debugging (tracing, performance instrumentation, etc), this is not a good idea. – John Saunders Nov 03 '10 at 20:15
  • @Downvoter: what's the problem with this answer? – John Saunders Aug 26 '11 at 19:39
  • 1
    @JohnSaunders that's not a right answer. You don't get the method name – David Oct 12 '12 at 10:15
  • 1
    @John Saunders He just means that this is more appropriate as a comment than an answer, since it doesn't give him an actual answer to his original question. – Panzercrisis Aug 19 '13 at 16:44