源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

在.NET中取得代码行数的方法

  • 时间:2020-07-15 19:53 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:在.NET中取得代码行数的方法
文章目的 介绍在.NET中取得代码行数的方法 代码
[u]复制代码[/u] 代码如下:
[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类
[u]复制代码[/u] 代码如下:
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 新方法
[u]复制代码[/u] 代码如下:
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 + ")"); }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部