How to find FULL name of calling method C#

前端 未结 5 1944
别那么骄傲
别那么骄傲 2020-12-31 00:24

How can I find the full name of a calling method in c#. I have seen solutions:

How I can get the calling methods in C#

How can I find the method that called

5条回答
  •  鱼传尺愫
    2020-12-31 01:06

    I think the best way to get the full name is:

     this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name;
    

    or try this

    string method = string.Format("{0}.{1}", MethodBase.GetCurrentMethod().DeclaringType.FullName, MethodBase.GetCurrentMethod().Name);  
    

    and if you want to display the most recent function call, you can use:

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

    , but if you want to display the tree of calling functions, you can do it like this:

    if (st.FrameCount >1)
    {
         // Display the highest-level function call 
         // in the trace.
         StackFrame sf = st.GetFrame(st.FrameCount-1);
         Console.WriteLine("  Original function call at top of call stack):");
         Console.WriteLine("      {0}", sf.GetMethod());
    }
    

    for more info

提交回复
热议问题