Answering "What method called me?" .NET runtime? Or are CallStack data read by code?

Suppose that there are methods A (), methodB (), and methodC ().

And the C () method is called at runtime.

Is it possible to find out why the C () method is called from which method?

I thought if CallStack could be read while doing some checks? If so, I think this should not be a big problem.

Any ideas?

Thanks!

+2
source share
2 answers

Use the StackTrace and StackFrame . For instance:

 StackTrace stackTrace = new StackTrace(); StackFrame[] stackFrames = stackTrace.GetFrames(); foreach (StackFrame stackFrame in stackFrames) { string method = stackFrame.GetMethod().Name; // do some stuff with method } 
+7
source

Yes, the call stack can be read at runtime using StackTrace.GetFrames .

+2
source

Source: https://habr.com/ru/post/957811/


All Articles