是否有可能获得调用另一种方法的名称方法?

时间:2009-03-24 14:35:14

标签: vb.net methods

这就是我所说的:

Public Shared Sub Test1()
    Test2()
End Sub

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

我想如果可以,System.Reflection会被利用吗?

有什么想法吗?

3 个答案:

答案 0 :(得分:7)

查看System.Diagnostics.StackFrame类。

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

作为旁注,你不应该依赖于你的调用者是谁,除非你正在编写调试器或用于记录目的,否则不应该使用它。

答案 1 :(得分:1)

Dim stackFrame As New Diagnostics.StackFrame(1)
stackFrame.GetMethod.Name.toString() & stackFrame.GetMethod.DeclaringType.FullName.tostring()

应该给你全名。

答案 2 :(得分:0)

这个问题应该有所帮助:
Can you use reflection to find the name of the currently executing method?

小心你如何做到这一点。如果你的方法是JIT内联的,你可能会看到错误的方法。

相关问题