Is there a way to dump the contents of the stack at runtime?
I am interested in both the parent functions information (name, parameters, line) which I know I can get with the StackTrace and StackFrame classes. However, I would also like to get the variables in the stack (local variables declared in the method that called the one is currently executing). Since the Visual Studio debugger can do this, I think there may be a way to also do it at runtime within the code. Is there such a way?
I suppose there are two ways of achieving this.
Your first option is to use an AOP framework to inject instrumentation code as a post-compilation step. This can be finely targeted and lets you do pretty much whatever you want, but works best when you can isolate the desired additional behaviors from the rest of the code. PostSharp is the leading contender in this area, but I'm sure there are others.
Your second option is to hook into the profiler API, which is what TypeMock Isolator and Microsoft's own Moles do. It basically gives you the means to intercept everything, but is quite program invasive and surely not a trivial task to implement. In fact, I'd not recommend this approach to anyone except to mention it for completeness.