System.Reflection

  • Thread starter Thread starter Raj Dhrolia
  • Start date Start date
R

Raj Dhrolia

Hi,
In one of my method, I want to know the class/function that
called/invoked this method.
i.e. i have a function named Method1() in class1. I want
"ClassX.MethodX()" in Method1(), when MethodX() of ClassX invokes Method1()
function
I think System.Reflection might provide some help.
Regards.
 
Hi,

You can use the System.Diagnostics.StrackTrace class to create a stack
trace. From the stack trace you can extract the preceeding stack frame
and call the GetMethod() member, this will return a MethodInfo object
which has a Name member that you can use to determine the calling method
name etc.

The following code sample should help (No error checking!!)

System.Diagnostics.StackTrace trace = new
System.Diagnostics.StackTrace();

if ( trace.FrameCount > 1 )
{
// Get caller frame
System.Diagnostics.StackFrame frame = trace.GetFrame(1);

// Dump calling methods name to debug window
System.Diagnostics.Debug.WriteLine( frame.GetMethod().Name );
}

Hope this helps

Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
 
Back
Top