>检索C#
中的调用方法名称知道启动当前方法的方法的名称对于调试和可靠的错误处理非常宝贵。 System.Reflection.MethodBase.GetCurrentMethod()
揭示有关当前方法的详细信息,但它没有识别呼叫者。
一个更简洁的解决方案是这条线:
// Capture the call stack using StackTrace StackTrace stackTrace = new StackTrace(); // Access the StackFrame for the calling method StackFrame callingFrame = stackTrace.GetFrame(1); // Output the calling method's name Console.WriteLine(callingFrame.GetMethod().Name);
此技术提供了一种精确的方法来获取调用方法的详细信息,绕开了堆栈跟踪解析的需求。
(new System.Diagnostics.StackTrace()).GetFrame(1).GetMethod().Name;
以上是如何在C#中获取调用方法的名称?的详细内容。更多信息请关注PHP中文网其他相关文章!