有没有办法在 C# 运行时检查堆栈变量? [英] Is there a way to examine the stack variables at runtime in C#?

查看:34
本文介绍了有没有办法在 C# 运行时检查堆栈变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在运行时转储堆栈的内容?

Is there a way to dump the contents of the stack at runtime?

我对可以通过 StackTrace 和 StackFrame 类获得的父函数信息(名称、参数、行)感兴趣.但是,我还想获取堆栈中的变量(在调用当前正在执行的方法中声明的局部变量).由于 Visual Studio 调试器可以做到这一点,我认为可能有一种方法可以在运行时在代码中做到这一点.有没有这样的方法?

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.

您的第一个选择是使用 AOP 框架注入检测代码作为编译后步骤.这可以很好地定位,让您几乎可以做任何您想做的事情,但是当您可以将所需的附加行为与其余代码隔离时,效果最好.PostSharp 是该领域的主要竞争者,但我敢肯定还有其他人.

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.

您的第二个选项是挂钩到分析器 API,这就是 TypeMock Isolator 和微软自己的 Moles 所做的.它基本上为您提供了拦截所有内容的方法,但它对程序的侵入性很大,而且实现起来肯定不是一项微不足道的任务.事实上,我不会向任何人推荐这种方法,只是为了完整起见.

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.

这篇关于有没有办法在 C# 运行时检查堆栈变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆