调试被动属性时自动调用属性getter [英] c# - Propery getter automatically called when debugging for passive property

查看:84
本文介绍了调试被动属性时自动调用属性getter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我正在使用一些具有属性的代码,该属性公开了被动更新/创建的字段,即仅在获取该字段时,并且有些标志指示该字段需要更新。这是代码:

I recently was working with some code with a property that exposed a field that was updated/created passively, that is only when getting it and some flag indicated that the field needed updating. This is the code:

static void Main(string[] args)
{
        var someClass  = new SomeClass();
        Console.WriteLine(someClass.ClassString);
        Console.ReadKey();
}

class SomeClass
{
    private bool _dirtyFlag;

    private String _classString;

    public String ClassString
    {
        get
        {
            Console.WriteLine("dirty flag value in getter: " + _dirtyFlag);
            Console.WriteLine("_classString value in getter: " + _classString);
            if (_dirtyFlag)
            {
                _classString = "new value";
                _dirtyFlag = false;
            }
            return _classString;
        }
    }

    public SomeClass()
    {

        SetDirtyFlag();

        Console.WriteLine("dirty flag value in constructor: " + _dirtyFlag);

        Console.WriteLine("_classString value in constructor: " + _classString);

    }


    public void SetDirtyFlag()

    {
        _dirtyFlag = true;
    }
}

在调试代码时,我发现了一些奇怪的行为:该标志值会自动从true设置为false ,即使未调用 ClassString (以某种方式从其他地方调用getter),也会更新 _classString 代码)。此外,在 ClassString getter中设置断点将不会显示首次调用getter的时间(这不是我的代码中的调用)。我会得到类似这样的输出:

When debugging the code I found some weird behavior: the flag value was set automatically from true to false and the _classString updated even when ClassString was not called (somehow the getter was called from somewhere else than the code). Additionally, setting a breakpoint in the ClassString getter would not show when the getter was called for the first time (which was not a call from my code). I would get outputs like:

dirty flag value in getter: True
_classString value in getter:
dirty flag value in constructor: False
_classString value in constructor: new value
dirty flag value in getter: False
_classString value in getter: new value
new value

是什么导致这种奇怪的行为?谁在我的代码执行之前调用吸气剂?

What is causing this weird behavior? Who is calling the getter before my code does?

推荐答案

调试器在调用您的吸气剂代码之前

The debugger is calling your getter code before your code does.

调试时,并且仅当您检查 ClassString 的值时(变量在Visual Studio的本地窗口中可见),则Visual Studio将尝试获取变量的值以在调试窗口中显示它们。然后在这种情况下调用getter并根据您的代码更新您的变量

When debugging your code and only if you are inspecting the value of ClassString (when the variable is visible in the Locals window of Visual Studio), then Visual Studio will try to get the value of the variables to show them in the debugging window. Then in this case the getter is called and your variables are updated according to your code.

同样值得注意的是如果有断点,则来自调试器的getter调用不会停止,因为断点仅适用于主执行线程。换句话说,代码的调试器调用将忽略断点。

It is also worth noting that this getter call from the Debugger does not stop if there is a breakpoint, since breakpoints only apply to the main execution thread. In other words, the debugger calls of code ignore the breakpoints.

这篇关于调试被动属性时自动调用属性getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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