How To Get The Calling Method And Type

Here are a couple of useful methods for getting information about the caller of a method. The first returns the calling method of the current method. The second returns the type of the caller. Both of these methods require declaring the System.Diagnostics namespace.

private static MethodBase GetCallingMethod()
{
  return new StackFrame(2, false).GetMethod();
}

private static Type GetCallingType()
{
  return new StackFrame(2, false).GetMethod().DeclaringType;
}

Pop Quiz! Why didn’t I apply the principle of code re-use and implement the second method like so?

public static Type GetCallingType()
{
    return GetCallingMethod().DeclaringType;
}

A virtual cigar and the admiration of your peers to the first person to answer correctly.

Technorati Tags: ,

What others have said

Requesting Gravatar... Aaron Lerch Aug 11, 2006 2:36 PM
# re: How To Get The Calling Method And Type
Pop Quiz: Because reusing the code in that way would modify the stack and give you the wrong return (it would return whatever type contains the GetCallingType() method).

In physics this would fall under the Heisenberg Uncertainty Principle where the act of measuring an electron's location actually modifies it's location.
Requesting Gravatar... jayson knight Aug 11, 2006 6:00 PM
# re: How To Get The Calling Method And Type
That sounds about right. I struggled with this very thing a while back on a project. The problem with using StackTrace or StackFrame is that IIRC your code has include .pdb files and needs to have debugging enabled (though doesn't necessarily have to be compiled in debug mode).

I personally like dealing with assembly references better, but it requires more code and is a little more tedious.
Requesting Gravatar... Ron Grabowski Aug 12, 2006 3:30 PM
# re: How To Get The Calling Method And Type
I wonder how important it is to mark the methods with this attribute so the compiler doesn't optimize the stack somehow:

[MethodImpl(MethodImplOptions.NoInlining)]
Requesting Gravatar... Dimitri Glazkov Aug 13, 2006 6:42 AM
# re: How To Get The Calling Method And Type
I think that's the code I wrote for the logging piece in Subtext, to determine where the logging call was made. Not sure if it's still there...
Requesting Gravatar... Haacked Aug 13, 2006 7:25 AM
# re: How To Get The Calling Method And Type
Dimitri, I took a look and indeed it is still there! Nice work!
# re: How To Get The Calling Method And Type
Dear Haacked,

There is littile typo in your code [It must be typo only ;) I know :) ]
return type of method GetCallingMethod should be MethodBase insted of MethodInfo.

happy blogging
Dhananjay

Requesting Gravatar... Haacked Aug 13, 2006 3:16 PM
# re: How To Get The Calling Method And Type
Thanks Dhananjay! You're absolutely right, it was just a typo. Funny because I actually compiled and tested the code, and forgot to copy and paste the correct code over. Doh!
# re: How To Get The Calling Method And Type
and here is the ans.

If you try to reuse this code, that means you are pusing one more method on the stack, And it will alwys return Type of GetCallingMethod() only.

codeing is fun
Dhananjay
Requesting Gravatar... Marcos Aug 15, 2006 1:43 AM
# re: How To Get The Calling Method And Type
I completely agree with Ron you must use the

[MethodImpl(MethodImplOptions.NoInlining)]

Because this is a perfect method to inline and the compile sure choice it has a candite.

Take into account that you calling method can be inlined too so you are returning the caller to the calling method,that is not what do you want.

The release mode optimizations can be a really mind cracker :P

just my 2 cents
Requesting Gravatar... Haacked Aug 15, 2006 3:37 AM
# re: How To Get The Calling Method And Type
Thanks, that is a good point. These are methods I typically use in debug code, but we do use them in some logging code that could be released in release mode. Good call!

What do you have to say?

(will show your gravatar)
Please add 3 and 8 and type the answer here: