直接从即时窗口拍摄:
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
(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
供参考,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
这是一个错误吗?如果直接转换为DateTime?
,则使用as DateTime?
投射也应该有效。
我确实通过使用没关系,没有很好地处理空值。无论如何,有很多方法可以解决这个奇怪的问题。reader.GetDateTime(reader.GetOrdinal("DateDue")) as DateTime?
找到了解决方法。
Repo演示此问题可在此处找到:https://github.com/jjoedouglas/exceptionAsDatetime
答案 0 :(得分:2)
这似乎是VS2015即时窗口中的错误。