Reputation: 53283
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!
Upvotes: 2
Views: 380
Reputation: 12092
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
}
Upvotes: 7
Reputation: 13011
Yes, the call stack can be read at runtime using StackTrace.GetFrames.
Upvotes: 2