在.NET中取得代碼行數(shù)的方法
更新時(shí)間:2014年06月03日 16:34:38 作者:
這篇文章主要介紹了在.NET中如何取得代碼行數(shù),需要的朋友可以參考下
文章目的
介紹在.NET中取得代碼行數(shù)的方法
代碼
[STAThread]
static void Main(string[] args)
{
ReportError("Yay!");
}
static private void ReportError(string Message)
{
StackFrame CallStack = new StackFrame(1, true);
Console.Write("Error: " + Message + ", File: " + CallStack.GetFileName() + ", Line: " + CallStack.GetFileLineNumber());
}
StackFrame(Int32, Boolean) 初始化與當(dāng)前堆棧幀之上的幀對(duì)應(yīng)的 StackFrame 類(lèi)的新實(shí)例,可以選擇捕獲源信息。
GetFileName :獲取包含所執(zhí)行代碼的文件名。 該信息通常從可執(zhí)行文件的調(diào)試符號(hào)中提取。
GetMethod :獲取在其中執(zhí)行幀的方法。
GetFileLineNumber :獲取文件中包含所執(zhí)行代碼的行號(hào)。 該信息通常從可執(zhí)行文件的調(diào)試符號(hào)中提取。
利用Exception(例外)的StackTrace類(lèi)
try
{
throw new Exception();
}
catch (Exception ex)
{
// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
}
.NET4.5 新方法
static void SomeMethodSomewhere()
{
ShowMessage("Boo");
}
...
static void ShowMessage(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
MessageBox.Show(message + " at line " + lineNumber + " (" + caller + ")");
}
介紹在.NET中取得代碼行數(shù)的方法
代碼
復(fù)制代碼 代碼如下:
[STAThread]
static void Main(string[] args)
{
ReportError("Yay!");
}
static private void ReportError(string Message)
{
StackFrame CallStack = new StackFrame(1, true);
Console.Write("Error: " + Message + ", File: " + CallStack.GetFileName() + ", Line: " + CallStack.GetFileLineNumber());
}
StackFrame(Int32, Boolean) 初始化與當(dāng)前堆棧幀之上的幀對(duì)應(yīng)的 StackFrame 類(lèi)的新實(shí)例,可以選擇捕獲源信息。
GetFileName :獲取包含所執(zhí)行代碼的文件名。 該信息通常從可執(zhí)行文件的調(diào)試符號(hào)中提取。
GetMethod :獲取在其中執(zhí)行幀的方法。
GetFileLineNumber :獲取文件中包含所執(zhí)行代碼的行號(hào)。 該信息通常從可執(zhí)行文件的調(diào)試符號(hào)中提取。
利用Exception(例外)的StackTrace類(lèi)
復(fù)制代碼 代碼如下:
try
{
throw new Exception();
}
catch (Exception ex)
{
// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
}
.NET4.5 新方法
復(fù)制代碼 代碼如下:
static void SomeMethodSomewhere()
{
ShowMessage("Boo");
}
...
static void ShowMessage(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
MessageBox.Show(message + " at line " + lineNumber + " (" + caller + ")");
}
相關(guān)文章
vs2017軟鏈接失效而導(dǎo)致無(wú)法進(jìn)入安裝界面的解決方法
這篇文章主要為大家詳細(xì)介紹了vs2017軟鏈接失效而導(dǎo)致無(wú)法進(jìn)入安裝界面的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
log4net教程日志分類(lèi)和自動(dòng)維護(hù)示例
log4net能不能按照功能分類(lèi)呢?如果通過(guò)配置不同的logger,然后功能根據(jù)不同的LoggerName加載Ilog實(shí)例,是可以做到。但由于這些功能的log配置差異性極小,也許僅僅就是文件名不同。于是想通過(guò)代碼進(jìn)行配置,下面把方法分享如下2014-01-01
探究ASP.NET Core Middleware實(shí)現(xiàn)方法
這篇文章主要介紹了探究ASP.NET Core Middleware實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
asp.net gridview多頁(yè)時(shí)的批量刪除
多余的代碼我就不貼了,有段時(shí)間沒(méi)寫(xiě).net了,最近又開(kāi)始寫(xiě)了,結(jié)果就一個(gè)gridview含多頁(yè)的批量刪除弄了我很久。貼上代碼,忘記再看下:2008-07-07
Asp.net內(nèi)置對(duì)象之Request對(duì)象(概述及應(yīng)用)
Request對(duì)象主要用于獲取來(lái)自客戶(hù)端的數(shù)據(jù),如用戶(hù)填入表單的數(shù)據(jù)、保存在客戶(hù)端的Cookie等,本文將圍繞Request對(duì)象,講解其的主要作用:讀取窗體變量、讀取查詢(xún)字符串變量、取得Web服務(wù)器端的系統(tǒng)信息。取得客戶(hù)端瀏覽器信息等等,感興趣的朋友可以了解下2013-02-02
MVC使用Memcache+Cookie解決分布式系統(tǒng)共享登錄狀態(tài)學(xué)習(xí)筆記6
這篇文章主要介紹了MVC使用Memcache+Cookie解決分布式系統(tǒng)共享登錄狀態(tài)學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

