1

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?

Anders
  • 12,088
  • 34
  • 98
  • 146

3 Answers3

7

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
  • 2,432
  • 19
  • 18
1
Dim stackFrame As New Diagnostics.StackFrame(1)
stackFrame.GetMethod.Name.toString() & stackFrame.GetMethod.DeclaringType.FullName.tostring()

Should give you the full name.

Brandon
  • 68,708
  • 30
  • 194
  • 223
0

This question should help:
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.

Community
  • 1
  • 1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794