How can I find the method that called the current method?

后端 未结 19 1708
猫巷女王i
猫巷女王i 2020-11-21 22:50

When logging in C#, how can I learn the name of the method that called the current method? I know all about System.Reflection.MethodBase.GetCurrentMethod(), but

相关标签:
19条回答
  • 2020-11-21 23:06

    Obviously this is a late answer, but I have a better option if you can use .NET 4.5 or newer:

    internal static void WriteInformation<T>(string text, [CallerMemberName]string method = "")
    {
        Console.WriteLine(DateTime.Now.ToString() + " => " + typeof(T).FullName + "." + method + ": " + text);
    }
    

    This will print the current Date and Time, followed by "Namespace.ClassName.MethodName" and ending with ": text".
    Sample output:

    6/17/2016 12:41:49 PM => WpfApplication.MainWindow..ctor: MainWindow initialized
    

    Sample use:

    Logger.WriteInformation<MainWindow>("MainWindow initialized");
    
    0 讨论(0)
  • 2020-11-21 23:07
    private static MethodBase GetCallingMethod()
    {
      return new StackFrame(2, false).GetMethod();
    }
    
    private static Type GetCallingType()
    {
      return new StackFrame(2, false).GetMethod().DeclaringType;
    }
    

    A fantastic class is here: http://www.csharp411.com/c-get-calling-method/

    0 讨论(0)
  • 2020-11-21 23:07

    For getting Method Name and Class Name try this:

        public static void Call()
        {
            StackTrace stackTrace = new StackTrace();
    
            var methodName = stackTrace.GetFrame(1).GetMethod();
            var className = methodName.DeclaringType.Name.ToString();
    
            Console.WriteLine(methodName.Name + "*****" + className );
        }
    
    0 讨论(0)
  • 2020-11-21 23:08

    In general, you can use the System.Diagnostics.StackTrace class to get a System.Diagnostics.StackFrame, and then use the GetMethod() method to get a System.Reflection.MethodBase object. However, there are some caveats to this approach:

    1. It represents the runtime stack -- optimizations could inline a method, and you will not see that method in the stack trace.
    2. It will not show any native frames, so if there's even a chance your method is being called by a native method, this will not work, and there is in-fact no currently available way to do it.

    (NOTE: I am just expanding on the answer provided by Firas Assad.)

    0 讨论(0)
  • 2020-11-21 23:09

    We can improve on Mr Assad's code (the current accepted answer) just a little bit by instantiating only the frame we actually need rather than the entire stack:

    new StackFrame(1).GetMethod().Name;
    

    This might perform a little better, though in all likelihood it still has to use the full stack to create that single frame. Also, it still has the same caveats that Alex Lyman pointed out (optimizer/native code might corrupt the results). Finally, you might want to check to be sure that new StackFrame(1) or .GetFrame(1) don't return null, as unlikely as that possibility might seem.

    See this related question: Can you use reflection to find the name of the currently executing method?

    0 讨论(0)
  • 2020-11-21 23:13

    In C# 5 you can get that information using caller info:

    //using System.Runtime.CompilerServices;
    public void SendError(string Message, [CallerMemberName] string callerName = "") 
    { 
        Console.WriteLine(callerName + "called me."); 
    } 
    

    You can also get the [CallerFilePath] and [CallerLineNumber].

    0 讨论(0)
提交回复
热议问题