[英]Cached property acts different in VS 2010 with breakoint
我有一堆类实现了缓存属性,这些属性返回从数据库中检索的数据集合。 我遇到的问题是,如果我在调试时设置了断点,VS 2010似乎会执行属性本身以显示字典项的计数。
如何阻止VS在准备好之前执行该属性? 提前致谢...
显示问题的示例:
public class CTest
{
private ICollection<int> _col = null;
public ICollection<int> col
{
get
{
if (this._col == null)
{
System.Diagnostics.Debug.Assert(false, "ASSERT!");
this._col = new Collection<int>();
this._col.Add(1);
this._col.Add(2);
this._col.Add(3);
}
return this._col;
}
}
}
CTest test = new CTest();
// A breakpoint on this line and no assert will fire
int nCount = test.col.Count;
// A breakpoint on this line and assert will fire
nCount = test.col.Count;
这是由自动属性评估引起的。 .NET开发的一般准则规定,属性评估应该快速且不会产生副作用 。 显然,ORM中的缓存和延迟加载等内容违反了这一原则,有利于增加可用性,但您遇到的是这种违规行为的一个后果。
要解决此问题,您需要在调试器选项对话框中关闭自动属性评估。 有关更多信息,请参阅此MSDN链接 。
(完全不相关的说明,标准.NET约定要求使用pascal-cased公共成员,例如属性和函数。考虑将属性名称[ Dic
而不是dic
]大写,并为其提供更具描述性的名称)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.