Possible Duplicate:
Can you use reflection to find the name of the currently executing method?
C# how to get the name of the current method from code可能重复:您可以使用反射来查找当前正在执行的方法的名称吗? C#如何从代码中获取当前方法的名称
For example:
例如:
void foo() {
Console.Write(__MYNAME__);
}
print: foo
打印:foo
it's possible do it in C#?
它可以在C#中实现吗?
2 个解决方案
#1
102
Try this:
尝试这个:
System.Reflection.MethodBase.GetCurrentMethod().Name
#2
14
You can check the stack trace
您可以检查堆栈跟踪
using System.Diagnostics;
// get call stack
StackTrace stackTrace = new StackTrace();
// get calling method name
Console.WriteLine(stackTrace.GetFrame(0).GetMethod().Name);
But beware, if the method is inlined you get the parent method name.
但要注意,如果方法是内联的,则获取父方法名称。
#1
102
Try this:
尝试这个:
System.Reflection.MethodBase.GetCurrentMethod().Name
#2
14
You can check the stack trace
您可以检查堆栈跟踪
using System.Diagnostics;
// get call stack
StackTrace stackTrace = new StackTrace();
// get calling method name
Console.WriteLine(stackTrace.GetFrame(0).GetMethod().Name);
But beware, if the method is inlined you get the parent method name.
但要注意,如果方法是内联的,则获取父方法名称。