日期:2014-05-18  浏览次数:21031 次

如何用反射得到调用当前方法的方法名称?
大家好,我想问一下如何用反射得到调用当前方法的方法名称
C# code
public void methodToBeCall()
{
    //想用反射或者methodMain的名字
}

public void methodMain()
{
    //Call methodToBeCall
    methodToBeCall();
}


我的问题是,如果我在methodMain里面调用methodToBeCall方法,我想在methodToBeCall方法里面用反射来得到methodMain的名字
我知道取得当前方法名是用这个: System.Reflection.MethodBase.GetCurrentMethod().Name
但是没找到取得调用当前方法名的办法啊~
能帮一下忙么?
谢谢了!

------解决方案--------------------
StackTrace

MethodInfo method = (MethodInfo)(new StackTrace().GetFrame(1).GetMethod());
Console.WriteLine(method.Name);