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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Reflection 6
Unit testing question 1
Wrong overload resolution ? 13
simplest thread safe pattern 15
inheritance issue 2
Multiple Interfaces 3
fundamental scope question 20
Reflection Array Parameter Error 1

Back
Top