有没有办法在 C# 中运行时检查堆栈变量?
有没有办法在运行时转储堆栈的内容?
我对父函数信息(名称、参数、行)感兴趣,我知道我可以通过 StackTrace 和 StackFrame 类获得这些信息。但是,我还想获取堆栈中的变量(在调用当前正在执行的方法中声明的局部变量)。由于 Visual Studio 调试器可以执行此操作,因此我认为可能有一种方法可以在代码中的运行时执行此操作。有这样的办法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想有两种方法可以实现这一目标。
您的第一个选择是使用 AOP 框架来注入检测代码作为编译后步骤。这可以有很好的针对性,让您几乎可以做任何您想做的事情,但当您可以将所需的附加行为与代码的其余部分隔离开时,效果最佳。 PostSharp 是该领域的领先竞争者,但我确信还有其他竞争者。
第二个选择是连接到探查器 API,这就是 TypeMock Isolator 和 Microsoft 自己的 Moles 所做的事情。它基本上为您提供了拦截所有内容的方法,但程序侵入性很强,而且实施起来肯定不是一项简单的任务。事实上,除了为了完整性而提及之外,我不会向任何人推荐这种方法。
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.