如何在 C# 像 C語言一樣使用 __LINE__ 取得所在行數?

 

It is uglier, but you can do something like this in C#using the StackTrace and StackFrame classes:

StackTrace st = new StackTrace(new StackFrame(true));
Console.WriteLine(" Stack trace for current level: {0}", st.ToString());
StackFrame sf = st.GetFrame(0);
Console.WriteLine(" File: {0}", sf.GetFileName());
Console.WriteLine(" Method: {0}", sf.GetMethod().Name);
Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());
Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());

Of course, this comes with some overhead.

資料來源: https://stackoverflow.com/questions/696218/do-line-file-equivalents-exist-in-c


文章標籤
創作者介紹
創作者 Frank 的頭像
Frank

經驗交流分享與備忘

Frank 發表在 痞客邦 留言(0) 人氣()