简体   繁体   English

在立即窗口中设置属性不设置?

[英]Setting property in Immediate Window not setting?

I have a problem in a project where I put a break point and start working with the properties of an object, and I can't set a value to a nullable int. 我在一个项目中遇到问题,我在其中设置了一个断点并开始使用对象的属性,并且我无法将值设置为可为空的int。 I can set it to null, but if I set it to anything then immediately check the value, it returns 1. 我可以将它设置为null,但如果我将其设置为任何值,则立即检查该值,它返回1。

I'm seeing this in the immediate window as well as the inspection window that pops up when you hover over a object when paused in debug mode. 我在即时窗口中看到这个,以及在调试模式下暂停时将鼠标悬停在对象上时弹出的检查窗口。 It also appears that this doesn't affect running code, just when you break and try to play with the values. 它似乎也不会影响正在运行的代码,只是在您中断并尝试使用这些值时。

I was able to reproduce it in a new console project: 我能够在一个新的控制台项目中重现它:

class Program
  {
    static void Main(string[] args)
    {
      var myValue = new TestItem()
      {
        NullableIntValue = null,
        StringValue = "My test value"
      };

      Console.WriteLine(string.Format("{0} is set to {1}", myValue.StringValue, myValue.NullableIntValue.HasValue ? myValue.NullableIntValue.Value.ToString() : "nothing"));

      myValue.NullableIntValue = 0;

      Console.WriteLine(string.Format("{0} is set to {1}", myValue.StringValue, myValue.NullableIntValue.Value));

      Console.ReadLine();

    }
  }

  public class TestItem
  {
    public int? NullableIntValue { get; set; }
    public string StringValue { get; set; }
  }

Paste that into a new console project, and put a break point at the "myValue.NullableIntValue = 0;" 将其粘贴到新的控制台项目中,并在“myValue.NullableIntValue = 0;”处放置一个断点。 line. 线。 Then run it. 然后运行它。

When the breakpoint hits, open the immediate window and set the value: 当断点命中时,打开立即窗口并设置值:

myValue.NullableIntValue = 123;
123

Then check the value: 然后检查值:

?myValue.NullableIntValue
1

Try it with something else: 尝试用其他东西:

myValue.NullableIntValue = null
null
?myValue.NullableIntValue
null

Set it to anything other than null and it seems to always be 1. 将它设置为null以外的任何值,它似乎总是1。

You can also hover over the breakpoint line "myValue" and let VS pop open the inspection window where you see all the values. 您也可以将鼠标悬停在断点线“myValue”上,让VS弹出检查窗口,您可以在其中看到所有值。 Go to NullableIntValue and set it to 100 (or anything) and hit enter and it immediately changes it to 1. 转到NullableIntValue并将其设置为100(或任何内容)并按Enter键并立即将其更改为1。

This only seems to behave this way for a property, ie if I change TestItem.NullableIntValue to a variable instead of a property it works fine. 这似乎只是对一个属性的行为,即如果我将TestItem.NullableIntValue更改为变量而不是属性,它工作正常。 And if I assign a value to the NullableIntValue in code and let it run it seems to take it no problem, just not during a break in debug. 如果我在代码中为NullableIntValue分配一个值并让它运行它似乎没有问题,只是在调试中断时没有。

Note, also tried this in VS 2013 and it takes the assignment just fine, so it seems limited to VS 2015, and I tried both C# 6 and C# 5, happens on both. 注意,在VS 2013中也尝试了这个并且它的任务很好,因此它似乎仅限于VS 2015,我尝试了C#6和C#5,两者都发生了。

Any ideas what could be causing this? 可能导致这种情况的任何想法?

This is a known bug in Visual Studio 2015 RTM. 这是Visual Studio 2015 RTM中的已知错误。 We fixed it in Visual Studio Update 1. Essentially marshalling complex value types (like nullables) from the debugger to debuggee process was broken. 我们在Visual Studio Update 1中修复了它。基本上,从调试器到调试对象进程的编组复杂值类型(如nullables)已被破坏。 If you don't want to install Update 1, you can work around it by going to Debug -> Options -> General and checking "Use the legacy C# and VB Expression Evaluators". 如果您不想安装Update 1,可以通过调试 - >选项 - >常规并选中“使用旧版C#和VB表达式评估程序”来解决它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM