国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

在.NET中取得代碼行數的方法

2024-07-10 12:43:16
字體:
來源:轉載
供稿:網友
文章目的

介紹在.NET中取得代碼行數的方法

代碼
代碼如下:
[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 :獲取包含所執行代碼的文件名。 該信息通常從可執行文件的調試符號中提取。

GetMethod :獲取在其中執行幀的方法。

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

利用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 + ")");
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中牟县| 滦平县| 云林县| 宽城| 广平县| 余姚市| 昆山市| 武乡县| 徐水县| 汝南县| 怀仁县| 黎川县| 福泉市| 巴东县| 雷州市| 金塔县| 安龙县| 汝州市| 沂水县| 濮阳市| 双鸭山市| 安乡县| 柳林县| 都江堰市| 湘潭市| 温泉县| 宣城市| 嘉义市| 开封市| 宕昌县| 凌海市| 金湖县| 西林县| 中超| 贞丰县| 大田县| 长阳| 罗源县| 通城县| 武定县| 克什克腾旗|