读书人

怎么用反射得到调用当前方法的方法名称

发布时间: 2012-01-19 20:57:58 作者: rapoo

如何用反射得到调用当前方法的方法名称?
大家好,我想问一下如何用反射得到调用当前方法的方法名称

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);

读书人网 >C#

热点推荐