在.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 类的新实例,可以选择捕获源信息。 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中取得代码行数的方法就介绍到这了。这个世界上从来没有一蹴而就的成功,如果没有踏实的努力,没有点滴的积累,再好的运气降落到你头上时,你也没有接住它的实力。早安!与您共勉,人生需要一步一个脚印!更多相关在.NET中取得代码行数的方法内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
vs2010显示代码行数的方法

自定义 DataList 显示数据行数的方法

DataList中TextBox onfocus调用后台void静态方法及获取相应行数

巧妙使用JQuery Clone 添加多行数据,并更新到数据库的实现代码

asp.net 长文章通过设定的行数分页