我如何在C#中的运行时跟踪变量? [英] How can I trace a variable at runtime in C#?

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

问题描述

如何跟踪变量的值,因为他们改变,在运行时,在C#中?我感兴趣的调试器,当我通过跟踪执行的步骤一个变量,只是我需要从我的代码后,它调用提供相同的功能。某种键值观测的,但对于各种变量(本地,类,静态等等),不仅属性。所以,基本上,收到通知时,变量值的变化。

How can I track a variable's values as they change, at runtime, in C#? I'm interested in the same functionality that the debugger provides when I'm tracing a variable through execution steps, only that I need to call upon it from my code. Some sort of key-value observing, but for all kinds of variables(local, class, static, etc), not only properties. So, basically, receive a notification when a variable's value changes.

推荐答案

您是从假设的工作,调试器可以跟踪变量变化。那可不一定。

You are working from the assumption that the debugger can track variable changes. It can't.

这可能与非托管代码时,处理器一直致力于允许设定数据断点调试寄存器。最多三个设置。当它看到特定内存位置获取写入它会产生一个硬件中断。这个本来非常有用的功能是不是在托管代码可但是。垃圾收集器是与它完全不兼容,它移动物体周围,给他们另一个地址。

It is possible with unmanaged code, the processor has dedicated debug registers that allow setting data breakpoints. Up to three are provided. It generates a hardware interrupt when it sees a particular memory location getting written. This otherwise very useful feature isn't available in managed code however. The garbage collector is completely incompatible with it, it moves objects around, giving them another address.

托管调试器不支持断点一个打时状态,使您转储到输出窗口的信息。该然而需要一个断点,它不能由在变量值的变化触发。它还确实因为调试器执行条件之前实际进入休息状态的减慢代码执行。

The managed debugger does support a "when hit" condition on a breakpoint, allowing you to dump info to the output window. That however requires a breakpoint, it cannot be triggered by a change in variable value. It also really slows down code execution since the debugger actually enters a break state before executing the condition.

最明显的地方放这样的断点在属性setter。这是你需要实现这个代码的功能是什么。你可以做你在二传手想要的任何东西,例如使用Trace类。

The obvious place to put such a breakpoint is in a property setter. Which is what you'll need to implement this feature in code. You can do anything you want in that setter, using the Trace class for example.

这篇关于我如何在C#中的运行时跟踪变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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