如何获取嵌套方法中执行的最后一个内部方法的名称?

时间:2013-07-03 12:04:30

标签: c# c#-4.0 c#-2.0

如何获取嵌套方法中执行的最后一个内部方法的名称?

假设我有一个Mainmethod(),它会调用Submethod1()Submethod2()然后会回到Mainmethod()

我需要最后执行的Submethod名称Submethod2() C#中是否有任何机制? 我不是在寻找Main方法名.....从Mainmethod()执行的最后一个方法 反射?

2 个答案:

答案 0 :(得分:0)

似乎你的问题已经得到了答案。

older post

这是你在找什么?

答案 1 :(得分:0)

您可以使用CallerMemberNameAttribute

吗?
class Program
{
    static void Main(string[] args)
    {
        MyMethod();
    }

    public static void MyMethod([CallerMemberName] String caller = null)
    {
        Console.WriteLine(caller); // Outputs Main
    }
}