Use Stacktrace to recognize the method in C#
In C#, determining the method of calling the current method is essential for failure and debugging. Although can provide information about the current method, it cannot display the method of calling it.
To get the name of the call method, you can use the System.Reflection.MethodBase.GetCurrentMethod()
class. Such capture execution stacks, including the calling method sequence of the current point.
Solution: StackTrace
Create a object:
StackTrace
Get the frame corresponding to the call method: <code class="language-csharp">StackTrace stackTrace = new StackTrace();</code>
<code class="language-csharp">StackFrame frame = stackTrace.GetFrame(1); // 跳过当前方法帧</code>
<code class="language-csharp">string callingMethodName = frame.GetMethod().Name;</code>
The above is the detailed content of How Can I Identify the Caller of a Method in C#?. For more information, please follow other related articles on the PHP Chinese website!