在.NET中如何取得代码行数

文章目的

介绍在.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 class that corresponds to a frame above the current stack frame, optionally capturing source information.">StackFrame class that corresponds to a frame above the current stack frame, optionally capturing source information."> StackFrame class that corresponds to a frame above the current stack frame, optionally capturing source information.">初始化与当前堆栈帧之上的帧对应的 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中如何取得代码行数,布布扣,bubuko.com

时间: 2024-08-04 13:29:02

在.NET中如何取得代码行数的相关文章

在.NET中怎样取得代码行数

文章目的 介绍在.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 + "

Visual Studio 中粗略的代码行数统计

在VS中, Ctrl + Shift +  F 调出查询窗, 输入正则表达式, 勾选"Use Regular Expressions", 选择好目标的文件类型, 点击Find All 自VS2012版本后: ^(?([^\r\n])\s)*[^\s+?/]+[^\n]*$ (?([^\r\n])\s)  代表any white space character ^\s+?/ 代表以/开头的行,即注释行 ^\n  代表行结尾 VS2012之前: ^:b*[^:b#/]+.*$ MSDN上关

使用vs的查找功能,简单大概的统计vs中的代码行数

VS强大的查找功能,可以使用正则表达式来进行查找,这里统计代码行数的原理就是: 在所有指定文件中进行搜索,统计匹配的文本行数. 但是匹配的行需要满足:非注释.非空等特殊非代码行. 使用Ctrl+Shift+F快捷键打开高级查找,可以设置查找内容为正则表达式(需要在下面勾选,同时可以设置查找的文件类型),查找范围请根据需要进行设置. 点击"查找全部"搜索整个解决方案,可以得到查找结果: 最下面会列出匹配行数,这个就是我们所谓的代码行数的估值了...不过这个值也太不准确了... 但是起码有

【原】Mac下统计任意文件夹中代码行数的工具——cloc

这里介绍一个Mac系统统计代码行数的工具cloc. 1.首先,安装homebrew,已安装的请跳过. 打开终端工具Terminal,输入下列命令.过程中会让你按RETURN键以及输入mac桌面密码,按照提示进行操作即可: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 2.利用homebrew下载并安装cloc. 继续在Terminal中输入: brew

【未解决】对于使用Windows的IDEA进行编译的文件,但无法在Linux系统中统计代码行数的疑问

在我学习使用Windows的IDEA的过程中,将代码文件转移到Linux虚拟机当中,但无法在Linux系统中统计代码行数. 注意:拷贝进虚拟机的文件均全能编译运行. 具体过程如下: root@yogile-VirtualBox:/alive/string# ls bin/ docs/ statistics.sh string/ work/ ##/shared/为虚拟机与宿主机的共享文件夹 root@yogile-VirtualBox:/alive/string# cp -r /shared/id

eclipse中统计代码行数或代码量

打开File Search对话框, 选中正则表达式,在搜索文本框输入 /n 文件名称输入 *.java 在范围里选中Enclosing projects 然后就可以统计出整个项目的代码行数.

统计代码文件中的实际有效行数,去掉空行、单行注释、多行注释

#coding=gbk import os #rootdir='f:\\pylianxi' def count_line_core(file_name): ##传入单个文件,统计行数,之后返回该文件的实际代码行数:区分utf-8.gbk有待优化 print('core_file_name:',file_name) lines_count=0 flag=True try: with open(file_name,'r',encoding='gbk') as fp: print('gbk file_

图文介绍如何在Eclipse统计代码行数

使用Eclipse可以方便的统计工程或文件的代码行数,方法如下: 1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File... 2.选中正则表达式(Regular expression),并在搜索文本框输入\n 3.在文件名中输入*或*.java 4.在范围里选中Enclosing projects 5.在Search窗口就会显示出项目或文件的代码行数

设置MyEclipse显示代码行数和设置字体的技巧

设置MyEclipse显示代码行数和设置字体的技巧 一个优秀的程序员一定会做到两点,避免bug和瞬间定位bug,显示代码的行数,能帮助我们及时发现错误,分析错误. 1.在MyEclipse菜单栏中找到Window选项,在其下拉菜单中选中Preferences选项,打开 2.在弹出的如下窗口中,在左侧选项列中选General,点击其下拉三角,找到其子选项,Editors 3.在Editors选项中,找到Text Editors选项选中,在右侧中找到Show line numbers,并勾选 4.关