如何获取嵌套方法中执行的最后一个内部方法的名称?
假设我有一个Mainmethod()
,它会调用Submethod1()
和Submethod2()
然后会回到Mainmethod()
。
我需要最后执行的Submethod
名称Submethod2()
C#中是否有任何机制?
我不是在寻找Main方法名.....从Mainmethod()执行的最后一个方法
反射?
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以使用CallerMemberNameAttribute
吗?class Program
{
static void Main(string[] args)
{
MyMethod();
}
public static void MyMethod([CallerMemberName] String caller = null)
{
Console.WriteLine(caller); // Outputs Main
}
}