如何从code当前方法的名称 [英] How to get the name of the current method from code

查看:89
本文介绍了如何从code当前方法的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你能行

  this.GetType()。全名

@Pasi萨沃莱恩的编辑礼貌

要获得


  

My.Current.Class


但我能打电话得到


  

My.Current.Class.CurrentMethod



解决方案

 堆栈跟踪ST =新的堆栈跟踪();
的StackFrame SF = st.GetFrame(0);MethodBase currentMethodName = sf.GetMethod();

或者,如果你想有一个辅助方法:

  [MethodImpl(MethodImplOptions.NoInlining)
公共字符串GetCurrentMethod()
{
    堆栈跟踪ST =新的堆栈跟踪();
    的StackFrame SF = st.GetFrame(1);    返回sf.GetMethod()名称。
}

更新与学分@stusmith。

I know you can do

this.GetType().FullName

Edit courtesy of @Pasi Savolainen

To get

My.Current.Class

But what can I call to get

My.Current.Class.CurrentMethod

解决方案

StackTrace st = new StackTrace ();
StackFrame sf = st.GetFrame (0);

MethodBase currentMethodName = sf.GetMethod ();

Or, if you'd like to have a helper method:

[MethodImpl(MethodImplOptions.NoInlining)]
public string GetCurrentMethod ()
{
    StackTrace st = new StackTrace ();
    StackFrame sf = st.GetFrame (1);

    return sf.GetMethod().Name;
}

Updated with credits to @stusmith.

这篇关于如何从code当前方法的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆