手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >在.NET中取得代码行数的方法
在.NET中取得代码行数的方法
摘要:文章目的介绍在.NET中取得代码行数的方法代码复制代码代码如下:[STAThread]staticvoidMain(string[]args...

文章目的

介绍在.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 + ")");

}

【在.NET中取得代码行数的方法】相关文章:

在ASP.NET中实现多文件上传的方法

c#中实现文件拖放打开的方法

ASP.NET中为GridView添加删除提示框的方法

在ASP.net中保存/取出图片入/从SQL数据库

ASP.NET中文件上传下载方法集合

asp.net水晶报表参数字段在代码中赋值的方法

使用.NET存储XML数据的方法

在asp.net中实现datagrid checkbox 全选的方法

在asp.net下实现Option条目中填充前导空格的方法

ASP.NET创建动态缩略图的方法

精品推荐
分类导航