开发过程中,我们有时候需要记录一下调用信息
1 public static void TraceMessage(string message, 2 [CallerMemberName] string memberName="", 3 [CallerFilePath] string sourceFilePath="", 4 [CallerLineNumber] int sourceLineNumber=0) 5 { 6 Console.WriteLine($"message:{message}\nmember name: {memberName}" + 7 $"\nsource file path: {sourceFilePath}\nsource line number: {sourceLineNumber}"); 8 } 9 10 11 public void DoSomething() 12 { 13 TraceMessage("事情开始起变化!"); 14 }
调用DoSomething的结果如下
message:事情开始起变化! member name: DoSomething //调用函数(准确讲应该是成员)名称 source file path: /Users/qinyuanlong/DotNet_core_x1/CallerMemberNameConsole/CallerMemberNameConsole/Program.cs //调用文件 source line number: 16 //调用行号
标签:CallerMemberName,调用,string,DoSomething,特性,file,message From: https://www.cnblogs.com/celestially98/p/16616552.html