ill*_*ant 72
Anders Hejlsberg在BUILD主题演讲中提出了新的API:
打印当前文件名,方法名称和行号
private static void Log(string text,
[CallerFilePath] string file = "",
[CallerMemberName] string member = "",
[CallerLineNumber] int line = 0)
{
Console.WriteLine("{0}_{1}({2}): {3}", Path.GetFileName(file), member, line, text);
}
Run Code Online (Sandbox Code Playgroud)
测试:
Log(".NET rocks!");
Run Code Online (Sandbox Code Playgroud)
输出:
Program.cs_Main(11):.NET摇滚!
这里发生了什么?
您可以使用optional参数定义方法并使用特殊属性对其进行装饰.如果在不传递实际参数的情况下调用方法(保留默认值) - Framework将为您填充它们.
use*_*016 34
这个答案已经过时了!有关最新信息,请参阅@taras的答案.
没有常数:(
你可以做的是更加丑陋:
string currentFile = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName();
int currentLine = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileLineNumber();
Run Code Online (Sandbox Code Playgroud)
仅在PDB文件可用时才有效.
| 归档时间: |
|
| 查看次数: |
24929 次 |
| 最近记录: |