Obtains the location information of the Code.

Source: Internet
Author: User

To obtain the location information of the Code, the class System. Diagnostics. StackFrame is the key. The source code is as follows:

1 Using System;
2 Using System. Collections. Generic;
3 Using System. Linq;
4 Using System. Text;
5 Using System. Diagnostics; // you need to know the source code location.
6  
7 Namespace ConsoleApplication1
8 {
9 Class Program
10 {
11 Static void FirstLevel ()
12 {
13 StackFrame stackFrame = new StackFrame (1, true );
14 Console. WriteLine (stackFrame. GetFileName (); // obtain the file name containing the executed code.
15 Console. WriteLine (stackFrame. GetFileLineNumber (). ToString (); // that is, the row number of the FirstLevel () where it is called
16 Console. WriteLine (stackFrame. GetFileColumnNumber (). ToString (); // that is, the column where the first letter "F" of the FirstLevel () is called
17 Console. WriteLine (stackFrame. GetMethod (). Module); // stackFrame. GetMethod () method for obtaining the frame to be executed.
18 Console. WriteLine (stackFrame. GetMethod (). ReflectedType );
19 Console. WriteLine (stackFrame. GetMethod (). ToString ());
20 }
21  
22 Static void SecondLevel ()
23 {
24 FirstLevel ();
25 }
26  
27 Static void Main (string [] args)
28 {
29 SecondLevel ();
30 Console. ReadKey ();
31 }
32 }
33 }

The execution result is as follows:

C: \ Users \ Administrator \ Desktop \ ConsoleApplication1 \ ConsoleApplication1 \ Program. cs
24
13
Leleapplication1.exe
Leleapplication1.program
Void SecondLevel ()

The analysis is as follows:

In the StackFrame (int skipFrames, bool fNeedFileInfo) constructor, skipFrames indicates the number of frames on the stack that have skipped the current frame, fNeedFileInfo indicates whether to obtain the file name, row number, and column number of the stack frame (in Visual Studio 2010, locate the call to the constructor StackFrame (1, true), press F12, you can see this information ).

So what is a stack frame? "Stack frame is the region (or space) allocated in the stack for the currently running function ). Input parameters and return address (the return address must be redirected after the function is completed. Note: Both the breakpoint of the main function and the internal storage unit used by the function (that is, the local variable of the function stored on the stack) are in the stack frame ." (Http://book.51cto.com/art/200804/70915.htm) for example, A () calls B (), B () calls C (), then B () is in the stack frame right in C () the previous one of the stack frames, that is, the number of frame hops between B () and C () is 1, and the number of frame hops between A () and C () is 2.

Now that you understand this, change "StackFrame stackFrame = new StackFrame (1, true);" in FirstLevel () to "StackFrame stackFrame = new StackFrame (2, true ); result:

C: \ Users \ Administrator \ Desktop \ ConsoleApplication1 \ ConsoleApplication1 \ Program. cs
29
13
Leleapplication1.exe
Leleapplication1.program
Void Main (System. String [])

Related Article

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.