Sma*_*ery 9 c# properties visual-studio
在过去,调试时我被"访问中的副作用"问题所困扰; 这意味着我已经初始化了缓存而没有触发断点(因为它已经在visual studio中暂停).所以我一直在想Visual Studio用来执行"无序"代码的机制,以便评估调试器中的属性.在我看来,这会绕过CLR吗?
所以问题是:从技术角度来看,这是怎么做到的?解释它的文章会有所帮助.
看起来 VS2012(可能还有更早的版本)使用“主线程”或者可能是遇到断点的线程来执行属性的 getter。
这是我的测试代码:
static class TestSideEffects
{
public static void Test()
{
Console.WriteLine("Main Thread: {0}", Thread.CurrentThread.ManagedThreadId);
var o = new TestSubject();
Console.WriteLine("Property Value: {0}", o.IntGetValueNoSe());
}
}
class TestSubject
{
private int _prop=0;
public int TheProperty
{
get
{
Console.WriteLine("Thread accessing the property: {0}", Thread.CurrentThread.ManagedThreadId);
return ++_prop;
}
}
public int IntGetValueNoSe(){return _prop; }
}
Run Code Online (Sandbox Code Playgroud)
我设置了两个断点:在 Test 方法的第三行和 getter 本身中,每次我将鼠标悬停在 o 实例上时 - 它都会执行 getter 而不会触发另一个断点。它使用相同的(在本例中是主线程)线程。
这是测试程序的输出:
Main Thread: 8
Thread accessing the property: 8
Thread accessing the property: 8
Thread accessing the property: 8
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
236 次 |
| 最近记录: |