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