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

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

    Maybe you are looking for something like this:

    StackFrame frame = new StackFrame(1);
    frame.GetMethod().Name; //Gets the current method name
    
    MethodBase method = frame.GetMethod();
    method.DeclaringType.Name //Gets the current class name
    

提交回复
热议问题