English 中文(简体)
Answering "Which method called me?" at the run-time in .NET? Or is CallStack data readable by the code?
原标题:

Presume that there are methodA() , methodB() and methodC().

And methodC() is called at the run-time.

Is is possible to know methodC() is called from what method?

I was thinking if CallStack can be read at the run-time for some checks? If yes, I think it should not be a big deal.

Any ideas?

Thanks!

最佳回答

Use the StackTrace and StackFrame classes. For example:

StackTrace stackTrace = new StackTrace();          
StackFrame[] stackFrames = stackTrace.GetFrames();

foreach (StackFrame stackFrame in stackFrames)
{
    string method = stackFrame.GetMethod().Name;
    // do some stuff with method
}
问题回答

Yes, the call stack can be read at runtime using StackTrace.Get­Frames.





相关问题
Call stack in compiled matlab

In matlab one can use dbstack to retrieve the call stack at the current time, however dbstack is not available in standalone compiled versions of matlab programs, is there an alternative to get the ...

Treating Warnings as Errors

I have a php application that I have just re-factored. Unfortunately it spewing out warnings like: Warning: preg_match() expects parameter 2 to be string, object given in /home/yacoby/dev/netbeans/...

Is there a call stack level limit?

I have a couple of colleagues looking at some bad code in Excel VBA, wondering is there a limit to the number of levels in a call stack

What does each entry in the Jmp_buf structure hold?

I am running Ubuntu 9.10 (Karmic Koala), and I took a look at the jmp_buf structure which is simply an array of 12 ints. When I use setjmp, and pass in a jmp_buf structure—4 out of 12 entries ...

热门标签