在.NET中怎样取得代码行数
2023-09-11 14:14:10 时间
文章目的
介绍在.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 + ")"); }
相关文章
- 200行代码,7个对象——让你了解ASP.NET Core框架的本质
- .NET压缩图片保存 .NET CORE WebApi Post跨域提交 C# Debug和release判断用法 tofixed方法 四舍五入 (function($){})(jQuery); 使用VUE+iView+.Net Core上传图片
- .NET框架- in ,out, ref , paras使用的代码总结 C#中in,out,ref的作用 C#需知--长度可变参数--Params C#中的 具名参数 和 可选参数 DEMO
- Ooui:在浏览器中运行.NET应用 Ooui.Wasm:浏览器中的.NET
- 通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core? .Net Web开发技术栈
- C# 插件热插拔 .NET:何时应该 “包装异常”? log4.net 自定义日志文件名称
- c# .net http Post请求 发送Json数据,经过微信签名测试有效
- ASP.NET MVC & Web API Brief Introduction
- ASP.NET Core的授权解析
- asp.net mvc跨域filter
- 采用Fiddler建立Asp.net webapi与Android/IOS调试环境
- Asp.net-MyFirstMVCProject详细解释
- .Net 5.0 通过IdentityServer4实现单点登录之oidc认证部分源码解析
- .net获取项目根目录方法集合
- C#-abo.net-Data Tracing in ADO.NET
- asp.net-Creating JavaScript objects from ASP.NET objects