Rakesh Rajan's blog

Thoughts on .NET, software and a few trivial things...

How to find out the immediate calling method

Use the StackFrame and StackTrace classes.

For example:


	public static void SourceMethod()
	{
		TargetMethod();
	}

	public static void TargetMethod()
	{
		StackTrace st = new StackTrace();

		// This will output 'SourceMethod'
		Console.WriteLine(st.GetFrame(1).GetMethod().Name);
	}


Note:
Code optimizations might give you different results.  Some methods may even be completely optimized out.  But this code should work fine without any optimizations.

Posted: May 02 2005, 10:55 PM by rakeshrajan | with 2 comment(s)
Filed under:

Comments

TrackBack said:

How to find out the immediate calling methodooeess
# May 24, 2005 1:52 AM

TrackBack said:

How to find out the immediate calling methodooeess
# July 22, 2005 3:22 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)