例如:
void foo() {
Console.Write(__MYNAME__);
}
打印:foo
有可能在 C# 中做到这一点吗?
例如:
void foo() {
Console.Write(__MYNAME__);
}
打印:foo
有可能在 C# 中做到这一点吗?
尝试这个:
System.Reflection.MethodBase.GetCurrentMethod().Name
您可以检查堆栈跟踪
using System.Diagnostics;
// get call stack
StackTrace stackTrace = new StackTrace();
// get calling method name
Console.WriteLine(stackTrace.GetFrame(0).GetMethod().Name);
但请注意,如果方法是内联的,您将获得父方法名称。