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

后端 未结 19 1729
猫巷女王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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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 );
        }
    

提交回复
热议问题