C#登录时,如何得知调用当前方法的方法名?我对 System.Reflection.MethodBase.GetCurrentMethod()
了如指掌,但我想在堆栈跟踪中更进一步。我已经考虑过解析堆栈跟踪,但我希望找到一种更清晰、更明确的方法,例如 Assembly.GetCallingAssembly()
但用于方法。
最佳答案
试试这个:
using System.Diagnostics;
// Get call stack
StackTrace stackTrace = new StackTrace();
// Get calling method name
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
一行:
(new System.Diagnostics.StackTrace()).GetFrame(1).GetMethod().Name
关于c# - 如何找到调用当前方法的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/171970/