How to get the number of lines of code in. Net

Source: Internet
Author: User
Tags stack trace

Article purpose

Introduced in. Ways to get the number of lines of code in net

Code

[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)Initializes a new instance of the StackFrame class corresponding to the frame above the current stack frame, optionally capturing source information.

GetFileName : Gets the file name that contains the code being executed. This information is typically extracted from the debug symbols of the executable file.

GetMethod : Gets the method in which the frame is executed.

Getfilelinenumber: Gets the line number in the file that contains the code being executed. This information is typically extracted from the debug symbols of the executable file.


StackTrace class with Exception (exception)

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 ();}

. A new method of 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 + ")");


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.