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.