tags:

views:

84

answers:

5

This is what I am talking about:

Public Shared Sub Test1()
    Test2()
End Sub

Public Shared Sub Test2()
    MsgBox(Test2.LastMethod) ' Test1
End Sub

I would imagine if this is possible, System.Reflection will be utilized?

Any thoughts?

+7  A: 

Look at the System.Diagnostics.StackFrame class.

 StackFrame frame = new StackFrame(1);
 MethodBase method = frame.GetMethod();
 Console.WriteLine(method.Name);

As a side note you shouldn't depend on who you caller is and shouldn't use this unless you are writing a debugger or for logging purposes.

devdimi
+1 for the answer, I'd +1 again for the caveat if I could
Binary Worrier
Another caveat: if the method is JIT-inlined it will return the wrong method.
Joel Coehoorn
+1  A: 
Dim stackFrame As New Diagnostics.StackFrame(1)
stackFrame.GetMethod.Name.toString() & stackFrame.GetMethod.DeclaringType.FullName.tostring()

Should give you the full name.

Brandon
A: 

Have a look at System.Diagnostics.StackTrace

PaulB
A: 

This question should help:
http://stackoverflow.com/questions/44153/can-you-use-reflection-to-find-the-name-of-the-currently-executing-method

Be careful how you do this. If your method is JIT-inlined you might see the wrong method.

Joel Coehoorn