[英]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.