The easiest,
Public class CallerTest {
Static
Public void Main (string [] args ){
Try {
Level1 ();
} Catch (Exception ex)
{
Console. WriteLine (ex );
}
Finally {
Console. ReadLine ();
}
}
Static
Void level1 ()
{
Level2 ();
}
Static
Void level2 ()
{
Stacktrace ST = new stacktrace ();
If (St. framecount
> 1 ){
Stackframe stframe =
St. getframe (1 );
Console. WriteLine (stFrame. GetMethod ());
}
}
}
If you enable the fNeedFileInfo switch, new StackTrace (true), you can trace the corresponding file name and row number.
Then we can observe from the object perspective,
Public class CallerTest {
Class
Callee {
Public void callAtCallee (){
StackTrace st = new StackTrace (1, true );
Console. WriteLine ("{0}. {1}", st. GetFrame (0). GetMethod (). ReflectedType. Name,
St. GetFrame (0). GetMethod (). Name );
}
}
Class
Caller {
Public void callAtCaller (Callee ee ){
Ee. callAtCallee ();
}
}
Static
Public void Main (string [] args ){
Try {
New Caller (). callAtCaller (new Callee ());
} Catch (Exception ex)
{
Console. WriteLine (ex );
} Finally {
Console. ReadLine ();
}
}
}
St. getframe (0 ). getmethod (). reflectedtype. name refers to the class where the corresponding methodbase is located, and St. getframe (0 ). getmethod (). name indicates the method name.
In this way, we can easily obtain the call class and method signature in the callatcallee method.
It seems difficult to obtain the specific object to call...
If you know, leave me a method. Thank you.