如何在运行时从方法中查找调用方法的方法名称?

Sto*_*orm 0 .net reflection

如何在运行时从方法中找到调用方法的方法名称?

例如:

Class A
{
    M1()
    {
        B.M2();
    }
}

class B
{
    public static M2()
    {
        // I need some code here to find out the name of the method that
        // called this method, preferably the name of the declared type
        // of the calling method also.
    }
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*ing 10

你可以试试:

using System.Diagnostics;

StackTrace stackTrace = new StackTrace();
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
Run Code Online (Sandbox Code Playgroud)