濮阳杆衣贸易有限公司

主頁 > 知識庫 > 在.NET中取得代碼行數(shù)的方法

在.NET中取得代碼行數(shù)的方法

熱門標(biāo)簽:浦發(fā)電話機器人提醒還款 400電話如何申請取消 太原400電話上門辦理 柳州電銷機器人公司 電銷語音機器人型號參數(shù) 騰訊地圖標(biāo)注手機 百度地圖怎樣做地圖標(biāo)注 昆明語音電銷機器人價格 征途美甲店地圖標(biāo)注
文章目的

介紹在.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)前堆棧幀之上的幀對應(yīng)的 StackFrame 類的新實例,可以選擇捕獲源信息。

GetFileName :獲取包含所執(zhí)行代碼的文件名。 該信息通常從可執(zhí)行文件的調(diào)試符號中提取。

GetMethod :獲取在其中執(zhí)行幀的方法。

GetFileLineNumber :獲取文件中包含所執(zhí)行代碼的行號。 該信息通常從可執(zhí)行文件的調(diào)試符號中提取。

利用Exception(例外)的StackTrace類
復(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 + ")");
}

標(biāo)簽:張家界 陽泉 天門 白山 新疆 江蘇 德陽 蘭州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《在.NET中取得代碼行數(shù)的方法》,本文關(guān)鍵詞  在,.NET,中,取得,代碼,行數(shù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《在.NET中取得代碼行數(shù)的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于在.NET中取得代碼行數(shù)的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    门源| 集贤县| 阆中市| 乃东县| 临武县| 农安县| 甘谷县| 烟台市| 米泉市| 刚察县| 铅山县| 安仁县| 永德县| 高碑店市| 天等县| 富平县| 晋州市| 仙游县| 来安县| 富锦市| 巨鹿县| 安泽县| 肥乡县| 宣武区| 台北市| 共和县| 卫辉市| 霍邱县| 都兰县| 石门县| 望都县| 鹤山市| 同江市| 东兰县| 普兰店市| 赤壁市| 襄汾县| 从江县| 营口市| 咸阳市| 井冈山市|