立即窗口 - 作为日期时间投射?抛出异常,但(datetime)没有

Mus*_*hin 14 c# datetime casting

直接从即时窗口采取:

reader["DateDue"] as DateTime? 收益率:

'reader["DateDue"] as DateTime?' threw an exception of type 'System.NullReferenceException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147467261
HelpLink: null
InnerException: null
Message: "Object reference not set to an instance of an object."
Source: null
StackTrace: null
TargetSite: null
Run Code Online (Sandbox Code Playgroud)

(DateTime?)reader["DateDue"] 收益率:

{1/26/2015 12:00:00 AM}
Date: {1/26/2015 12:00:00 AM}
Day: 26
DayOfWeek: Monday
DayOfYear: 26
Hour: 0
Kind: Unspecified
Millisecond: 0
Minute: 0
Month: 1
Second: 0
Ticks: 635578272000000000
TimeOfDay: {System.TimeSpan}
Year: 2015
Run Code Online (Sandbox Code Playgroud)

并参考,reader["DateDue"]产量:

{1/26/2015 12:00:00 AM}
Date: {1/26/2015 12:00:00 AM}
Day: 26
DayOfWeek: Monday
DayOfYear: 26
Hour: 0
Kind: Unspecified
Millisecond: 0
Minute: 0
Month: 1
Second: 0
Ticks: 635578272000000000
TimeOfDay: {00:00:00}
Year: 2015
Run Code Online (Sandbox Code Playgroud)

这是一个错误吗?如果直接铸造到DateTime?工程,那么铸造也as DateTime?应该工作.

我确实通过使用找到了解决这个问题的方法reader.GetDateTime(reader.GetOrdinal("DateDue")) as DateTime?.没关系,没有很好地处理空值.无论如何,有很多方法可以解决这个奇怪的问题.

可在此处找到回复演示此问题:https://github.com/jjoedouglas/exceptionAsDatetime

Mus*_*hin 2

这似乎是 VS2015 即时窗口中的一个错误。