2010-07-20 33 views
1

場景:vs2008我可以得到行號@ *的值嗎?

我最近了解到Mark Russinovich的有用的DebugView *工具。
隨着DebugView中,即使有VS2008關閉,我可以使用語句這樣的:

System.Diagnostics.Debug.WriteLine("some text =. " + some_variable); 

我想修改上述,在編譯時,對包括所述源代碼的行號:

System.Diagnostics.Debug.WriteLine("nnnn: some text =. " + some_variable); 

其中nnnn代表源代碼行號。 AFAIK,要做到這一點,vs2008需要某種形式的元變量,如@linenumber,用於當前編譯時源代碼行號。即,如果該語句的Debug.WriteLine
是在第十行,那麼這樣的元變量將== 10.

因此,調試語句諸如粗糙上面的例子可以在多個地方,它被用來很容易識別每個調試語句的源代碼位置。

問題是有可以在編譯時引用變量元?

P.S .:我無法通過Google找到此問題的答案,也無法通過SO搜索功能找到答案。

+0

我們這裏不做簽名,或者,「你好」,或「謝謝」。這是一個問答網站,我們沒有討論。 – 2010-07-20 19:48:04

+0

FWIW,c#5,.NET 4.5中的新增功能:http://msdn.microsoft.com/zh-cn/library/system.runtime.compilerservices.callerlinenumberattribute.aspx&http://msdn.microsoft.com/zh-cn/我們/圖書館/ system.runtime.compilerservices.callerfilepathattribute.aspx&http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute.aspx ...不完全是我在找三多年前;至少對我來說至少有用。 – gerryLowry 2013-08-05 03:43:45

回答

0

即使我希望有更快的獲得此信息的方法10月16日,這裏有一個方法,你可以使用System.Diagnostics命名空間中的類StackTrace獲取文件名和行號:

StackTrace st = new StackTrace(new StackFrame(true)); 
string fileName = st.GetFrame(0).GetFileName(); 
string lineNumber = st.GetFrame(0).GetFileLineNumber().ToString(); 
string fileNameAndLineNbr = fileName + ":" + lineNumber; 

System.Diagnostics.Debug.WriteLine(fileNameAndLineNbr + ": some text =. " + some_variable); 
1

這是我所知道的獲得實際線路正確的最佳方式(也就是說,它的行權,因爲它是在線)

Debug.WriteLine(" hi there I am line number [" + (new StackTrace(new StackFrame(true)).GetFrame(0).GetFileLineNumber().ToString()) + "]"); 
相關問題