简体   繁体   English

如何防止Visual Studio 2015的调试器评估带有副作用的静态属性?

[英]How do I prevent Visual Studio 2015's debugger from evaluating static properties with side effects?

I have a singleton class that lazy creates its instance when a property is first called (property side effect). 我有一个单例类,该类在第一次调用属性时会懒惰地创建其实例(属性副作用)。

namespace singletontest2
{
    using System.Diagnostics;
    using MySingleton = Singleton<MyClass>;

    public class MyClass
    {
        private int val = 42;

        public MyClass()
        {
        }
    }

    public static class Singleton<T> where T : new()
    {
        private static T t;

        public static bool IsNull
        {
            get
            {
                return t == null;
            }
        }

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public static T Instance
        {
            get
            {
                if (t == null)
                    t = new T();
                return t;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var a = MySingleton.IsNull;
            var d = MySingleton.Instance;
        }
    }
}

If I place a break point in the Instance property getter and on the var d line, the t member will already be initialized when I step into the Instance getter. 如果我在Instance属性getter和var d行中放置一个断点,那么当我进入Instance getter时, t成员将已经初始化。

If I move the break point from var d to the next line, the t is null when I debug into the Instance getter. 如果将断点从var d移到下一行,则当我调试Instance getter时, t为null。

I was under the impression the DebuggerBrowsable attribute would prevent this but it seems to not make any difference. 我的印象是DebuggerBrowsable属性可以防止这种情况,但似乎没有任何区别。

HOWEVER, if I eliminate the using MySingleton = Singleton<MyClass>; 但是,如果我取消using MySingleton = Singleton<MyClass>; above and call Singleton<MyClass>.Instance; 以上并调用Singleton<MyClass>.Instance; instead of MySingleton.Instance; 而不是MySingleton.Instance; , then the member t is null when I enter the Instance getter regardless of DebuggerBrowsable which is what I want. ,则无论我要使用DebuggerBrowsable为何,当我输入Instance getter时成员t为null。

Why is the using directive enabling the debugger to evaluate that property before the code is actually executed? 为什么using指令允许调试器在实际执行代码之前评估该属性?

如此处所提倡的那样,关闭“ 工具”->“选项”->“调试”->“启用属性评估和其他隐式函数调用”https : //msdn.microsoft.com/zh-cn/library/0taedcee.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio Community 2015调试器在条件断点处以“不支持本机方法的评估”结束 - 我该如何解决? - Visual Studio Community 2015 debugger ends at conditional breakpoint with “Evaluation of native methods is not supported” - how do I fix? 如何防止 Visual Studio 在设计器上触发事件? - How do I prevent Visual Studio from firing events on the designer? 如何访问动态按钮的事件处理程序? Visual Studio 2015 WPF - How do I access a dynamic button's event handler? Visual Studio 2015 WPF Visual Studio:让调试器意识到函数不会导致“副作用” - Visual Studio: Make debugger aware that a function doesn't cause “side effects” 如何在Visual Studio中搜索注释? (VS 2015) - How do I search for comments in Visual Studio? (VS 2015) 如何在Visual Studio 2015中创建可执行文件 - How do I create an executable in Visual Studio 2015 如何在Visual Studio 2015中禁用C#6支持? - How do I disable C# 6 Support in Visual Studio 2015? 如何在Visual Studio 2015中不打印折叠的代码区域 - How do I not print collapsed code regions in Visual Studio 2015 如何在Visual Studio 2015中禁用命名空间缩写? - How do I disable namespace abbreviation in Visual Studio 2015? 如何在Visual Studio 2015中重新生成Designer文件 - How do I regenerate designer File in Visual Studio 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM