如何在没有硬代码字符串的情况下打印函数名称?

How to print function name without hard code string?

c语言有FILE/LINE这样的宏来打印文件名和代码行号

那么C#语言是如何实现同样的目标的,是否可以做到,或者需要汇编包?

非常感谢。

您可以使用 StackFrame,但我会谨慎地这样做,它会以性能成本为代价,并且可能会在发布时给您带来问题。

Represents a stack trace, which is an ordered collection of one or more stack frames.

var stackTrace = new StackTrace(0, true);
var sf = stackTrace.GetFrame(0);
Console.WriteLine("FileName: {0}", sf.GetFileName());
Console.WriteLine("Line Number:{0} ",sf.GetFileLineNumber());
Console.WriteLine("Function Name:{0}",sf.GetMethod ());