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

查看:429
本文介绍了有没有办法在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.

您的第二个选项是挂钩到Profiler 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天全站免登陆