zoukankan      html  css  js  c++  java
  • C# 获得当前方法 和 方法调用链 的 方法

    一个获得方法名的方法,depth表示调用此方法的回溯深度。

    比如,A方法调用B方法,B方法调用GetCurrentMethodFullName(2),那么得到的结果是A方法的全名(namespace+class名+method名)

    若要获得当前方法,depth应为0

            internal static string GetCurrentMethodFullName(int depth)
            {
                try
                {
                    StackTrace st = new StackTrace();
                    string methodName = st.GetFrame(depth).GetMethod().Name;
                    string className = st.GetFrame(depth).GetMethod().DeclaringType.ToString();
                    return className + "." + methodName;
                }
                catch
                {
                    return null;
                }
            }

    如果不需要知道方法调用链,那么其实用MethodBase.GetCurrentMethod()就可以得到当前方法,可以用Name得到名字,用DeclaringType.ToString()得到namespace和class名。

    MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();

  • 相关阅读:
    poj3167
    poj2752 bzoj3670
    poj2886
    poj3294
    [luoguP2564][SCOI2009]生日礼物(队列)
    [luoguP1866]滑动窗口(单调队列)
    [luoguP1198][JSOI2008] 最大数(线段树 || 单调栈)
    [HDU4348]To the moon(主席树)
    [luoguP1168]中位数(主席树+离散化)
    [HDU4417]Super Mario(主席树+离散化)
  • 原文地址:https://www.cnblogs.com/swarb/p/9924251.html
Copyright © 2011-2022 走看看