【发布时间】:2012-04-25 04:59:38
【问题描述】:
可能重复:
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
例如:
void foo() {
Console.Write(__MYNAME__);
}
打印:foo
可以用 C# 实现吗?
【问题讨论】:
-
在 .Net 4.5 中,您可以使用 CallerMemberNameAttribute 来获取调用者的姓名。请参阅msdn.microsoft.com/en-us/library/… ...然后您可以将函数的主体包装在匿名函数中,如 ([CallerMemberName] string functionName = "")=>{ }。在接受的答案中使用反射方法的问题是(1)函数可能被内联,和/或(2)如果函数名称是非公开的并且代码被混淆,则函数名称可能被混淆。
标签: c# .net system.reflection