简体   繁体   English

查看它们时,Visual Studio调试值会发生变化吗?

[英]Visual studio debug values changing when looking into them?

So i've been writing some unit tests, and there's one test that fails that shouldn't fail, so I started debugging the test. 因此,我一直在编写一些单元测试,并且有一个失败的测试应该不会失败,因此我开始调试该测试。 I looked through the properties and after the debugging was finished, the test passed. 我浏览了属性,调试完成后,测试通过了。 weird? 奇怪的? So, I ran the test again, and it failed again. 因此,我再次运行了测试,但再次失败了。

Turns out, the properties(in this case .Selected of an listview-item) actually changed when I looked into it, but it didn't when I don't..... 事实证明,当我查看属性时,属性(在本例中为listview-selected)实际上发生了变化,但当我不这样做时却没有发生变化。

Here's a video I made to show this weird issue. 这是我制作的视频,目的是展示这个奇怪的问题。

https://youtu.be/OsUhh-MlOoU https://youtu.be/OsUhh-MlOoU

The documentation for SelectedItems has this to say: SelectedItems的文档中这样说:

The SelectedItems property will not contain any items if the property is accessed before the ListView handle is created. 如果在创建ListView句柄之前访问了SelectedItems属性,则该属性将不包含任何项目。

Since you're in a test method I would assume you don't actually show the form, nor the listview, on the screen which would require a handle. 由于您使用的是测试方法,因此我假设您实际上并没有在需要手柄的屏幕上显示表单或Listview。 As such, the listview doesn't get a handle until you force it to. 这样,除非强制使用,否则listview不会获得句柄。

The act of expanding the listview object in the debug inspector makes the SelectedItems property suddenly contain the element after all which indicates to me that one of the other properties on the ListView type has a side-effect of triggering this creation of a handle. 在调试检查器中扩展listview对象的行为使SelectedItems属性突然包含该元素,这向我表明ListView类型上的其他属性之一具有触发此句柄创建的副作用。

Here's a small example that demonstrates a similar situation: 这是一个演示类似情况的小示例:

void Main()
{
    var x = new Test();
    Console.WriteLine(x.GoodProperty); // place a breakpoint on this line
    Console.WriteLine(x.BadProperty);
    Console.WriteLine(x.GoodProperty);
}

public class Test
{
    public int GoodProperty { get; set; }
    public int BadProperty => GoodProperty++;
}

If you place a breakpoint on the indicated line, then use your mouse to just mouse over the x.GoodProperty expression in the same line, you'll get 0. You can do this over and over again and it still produces 0. 如果在指定的行上放置一个断点,然后使用鼠标将鼠标悬停在同一行的x.GoodProperty表达式上,则会得到0。您可以一遍又一遍地执行此操作,它仍然会产生0。

However, every time you mouse over the x.BadProperty expression, x.GoodProperty will also increase by 1. 但是,每次将鼠标悬停在x.BadProperty表达式上时, x.GoodProperty也会增加1。

The exact same thing would happen if you mouse over x and expand it to show all properties. 如果将鼠标悬停在x并将其展开以显示所有属性,则会发生完全相同的事情。 Depending on whether GoodProperty or BadProperty will evaluate first when you expand x , the expanded view may or may not show a changed GoodProperty value. 取决于在展开x时是首先评估GoodProperty还是BadProperty ,展开的视图可能会或可能不会显示更改后的GoodProperty值。

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

相关问题 使用代码在 Visual Studio 调试控制台中更改颜色 - Changing color in Visual Studio Debug Console With Code Visual Studio /反射/序列化:是否可以从调试会话中导出值以将其用于单元测试? - Visual Studio / Reflection / Serialization: Can I export values from a debug session to use them for unit testing? Visual Studio-在不处于调试模式时逐步进行操作 - Visual Studio - step through when not in debug mode Visual Studio 调试符号 - Visual Studio Debug Symbols 调试 Visual Studio 设计器 - Debug Visual Studio Designer Visual Studio 从 Debug 目录中删除所有 dll、pdb、xml 文件,并且在我尝试发布应用程序时需要发布 - 如何恢复它们? - Visual Studio deletes all dll, pdb, xml files from Debug directory and requires publishing when I tried to publish application - how to restore them? 我能否确定Visual Studio 2012何时处于“调试与生产”阶段? - Can I tell when Visual Studio 2012 is in Debug vs Production? 调试附加到进程时出现 Visual Studio 2019 错误 - Visual Studio 2019 Error when debug attach to process MsBuild在我生成时会删除依赖项dll,然后在Visual Studio中进行调试 - MsBuild deletes dependency dlls when I build, then debug in Visual Studio Visual Studio 2017-在调试模式下无法查看变量 - Visual Studio 2017 - Cant view variables when in debug mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM