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

后端 未结 19 1749
猫巷女王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: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/

提交回复
热议问题