校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

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

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

熱門標簽:浦發(fā)電話機器人提醒還款 400電話如何申請取消 太原400電話上門辦理 柳州電銷機器人公司 電銷語音機器人型號參數(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) 初始化與當前堆棧幀之上的幀對應的 StackFrame 類的新實例,可以選擇捕獲源信息。

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

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

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

利用Exception(例外)的StackTrace類
復制代碼 代碼如下:

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 + ")");
}

標簽:張家界 陽泉 天門 白山 新疆 江蘇 德陽 蘭州

巨人網(wǎng)絡通訊聲明:本文標題《在.NET中取得代碼行數(shù)的方法》,本文關鍵詞  在,.NET,中,取得,代碼,行數(shù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。
  • 相關文章
  • 下面列出與本文章《在.NET中取得代碼行數(shù)的方法》相關的同類信息!
  • 本頁收集關于在.NET中取得代碼行數(shù)的方法的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 大悟县| 上蔡县| 宁安市| 陵川县| 那坡县| 广安市| 鄂州市| 塔城市| 张家界市| 石狮市| 丹寨县| 科技| 中山市| 花莲市| 大化| 上杭县| 乌拉特后旗| 邯郸县| 玉环县| 永济市| 中西区| 山东| 富民县| 永和县| 苍溪县| 上饶市| 达拉特旗| 巴青县| 鹿泉市| 嘉荫县| 和平区| 阿巴嘎旗| 双江| 会昌县| 兴仁县| 嘉禾县| 赫章县| 福建省| 永年县| 顺义区| 赤水市|