Analysis of stacktrace

Source: Internet
Author: User

When learning function calls, we know that each function has its own stack space. When a function is called, a new stack space is created. Then, a function call stack is formed by nested function calls. In C #, use stacktrace to record this stack. You canProgramUse stacktrace to obtain information about the current stack.

Class Program
{
Static   Void Main ( String [] ARGs)
{
Program= NewProgram ();
A. funca ();
Console. Readline ();
}
Int Funca ()
{
Funcb ();
Return 0;
}

Private   Void Funcb ()
{
Methodinfo method0 = (Methodinfo )( New Stacktrace (). getframe ( 0 ). Getmethod ());
Methodinfo Method1 = (Methodinfo )( New Stacktrace (). getframe ( 1 ). Getmethod ());
Methodinfo method2 = (Methodinfo )( New Stacktrace (). getframe ( 2 ). Getmethod ());

Console. writeline ( " Current Method is: {0} " , Method0.name );
Console. writeline ( " Parent method is: {0} " , Method1.name );
Console. writeline ( " Grandparent method is: {0} " , Method2.name );
}
}

The output result of the program is:
Current Method is: funcb
Parent method is: funca
Grandparent method is: Main

Call getframe to obtain the stack space. The index parameter indicates the stack space level. 0 indicates the current stack space. 1 indicates the stack space at the upper level, and so on.
Besides obtaining method information, you can also call the member functions of the stackframe class to obtainCode. For details, refer to an example on msdn.

Msdn example
// Display the stack frame properties.
Stackframe SF = St. getframe (I );
Console. writeline ( " File: {0} " , SF. getfilename ());
Console. writeline ( " Line number: {0} " ,
SF. getfilelinenumber ());
// Note that the column number defaults to zero
// When not initialized.
Console. writeline ( " Column number: {0} " ,
SF. getfilecolumnnumber ());
If (SF. getiloffset () ! = Stackframe. offset_unknown)
{
Console. writeline ("Intermediate Language offset: {0}",
SF. getiloffset ());
}
If (SF. getnativeoffset () ! = Stackframe. offset_unknown)
{
Console. writeline ("Native offset: {0}",
SF. getnativeoffset ());
}

In DuduArticleIn application of. NET Programming (4), attribute is used to obtain the information of the function at the upper level through stacktrace, that is, the information of addcustomer, to further create sqlparameter. For more information, see its 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.