views:

309

answers:

6

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.
  }
}
+6  A: 

You can try:

using System.Diagnostics;

StackTrace stackTrace = new StackTrace();
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
weichsel
thanks ! it works just fine.
Binder
+1: cool, didn't know that
Juri
A: 

Hi,

I think you are looking for:

using System.Diagnostics;

StackTrace stackTrace = new StackTrace();
stackTrace.GetFrame(1).GetMethod().Name;
James
-1 Duplicate of higher voted older item (and also -1d the question on the same basis)
Ruben Bartelink
@Ruben - Never realised someone posting an answer faster than you deserved a downvote!
James
@James: 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
A: 

You can do this by displaying the call stack. 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()); }

daed
Whau, that is probably the most interesting way I have ever seen anyone write to the console :-)
Jørn Schou-Rode
That looks like complicator code... http://thedailywtf.com/Articles/The_Complicator_0x27_s_Gloves.aspx
Greg D
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 :)
daed
A: 

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

TheSimon
A: 

Thanks for the response guys, just one more thing, any idea how much of a performance hit would be incurred, any numbers? thanks in adv.

Binder
I don't have any numbers, but for sure reflection is a performance killer. It allows to do really cool things, but is not really suggested for performance aware systems.
Juri
Not just the reflection, but generating the stack trace is also going to be a performance hit.
Nate
The performance question concerns me. A technique like this should probably be used for diagnostic purposes only, unless you're writing some sort of instrumentation library.
Greg D
This is a very bad idea. The people who answered you did not do you any favors.
John Saunders
Hi John, can you please explain why would you say such a thing?
Binder
A: 

Better not to use StackFrame, becouse there are some .NET security issues - if code not fully trusted expression "new StackFrame()" will throw security exception.

To get current method use MethodBase.GetCurrentMethod().Name

About getting calling method see: Getting calling method owner

ALOR
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. :)
Binder
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