This is a very frustrating and bizarre issue and I would appreciate any advice on 开发者_如何学编程how to resolve it.
I have an object with a private variable:
private DateTime _maturityDate = DateTime.MaxValue;
It has a corresponding property:
public DateTime MaturityDate { get; set; }
I have a method which updates the data in the database called UpdateInstrumentBase(). The property is set from a DateTimePicker control on a Windows form. It is set via code, not via data binding:
((Instrument)instrumentBS.DataSource).MaturityDate = dateTimePicker9.Value;
This correctly sets the value:
(I can't post images so you will have to trust me that it does)
However - and this is the truly bizarre problem - when you step INSIDE the object, this is what the property is set to. Even trying to output it in the immediate window or by using a console.writeline results in the following:
? _maturityDate {System.DateTime} Date: Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized.
I've tried passing the date value in as a string, then converting to DateTime as a workaround but any access to ANY DateTime property or variable - not just this one - inside this object results in this error. I've searched high and low but I'm not even sure if this error message is relevant or useful.
I'm using .NET Framework 3.5 SP1 in Visual Studio 2008 version 9.0.21022.8 if that's relevant.
I'm stumped. The object is quite complex so I'm hesitant to post the entire thing, but if anyone has any ideas I will post the relevant code.
Huge and gracious thanks in advance!
This is not a bug in your code. What's happening here is the C# debugger is attempting to evaluate an expression and is getting back a return value of CORDBG_E_ILLEGAL_AT_GC_UNSAFE_POINT
or CORDBG_E_ILLEGAL_IN_OPTIMIZED_CODE
from the CLR. These are error codes that indicate it's not possible to evaluate an expression in the current context and has little to do with the actual user code.
Mike Stall has a good breakdown on these messages and why they occurr that may be worth a read.
- http://blogs.msdn.com/b/jmstall/archive/2005/11/15/funceval-rules.aspx
Unfortunately though there is little you can do to work around this problem. Unless the issue is you are debugging optimized, in which case turning off optimizations will fix the problem.
Observation:
Based on what you've posted above,
public DateTime MaturityDate { get; set; }
is an autoproperty (http://msdn.microsoft.com/en-us/library/bb384054.aspx) and should have no connection to _maturityDate.
Am I missing something?
精彩评论