可为空的对象必须有一个值 - nullable object must have a value

10/14 03:58
阅读数 0

问题:

There is paradox in the exception description: Nullable object must have a value (?!)异常描述中有悖论:可空对象必须有值(?!)

This is the problem:这就是问题:

I have a DateTimeExtended class, that has我有一个DateTimeExtended类,它有

{
  DateTime? MyDataTime;
  int? otherdata;

}

and a constructor和一个构造函数

DateTimeExtended(DateTimeExtended myNewDT)
{
   this.MyDateTime = myNewDT.MyDateTime.Value;
   this.otherdata = myNewDT.otherdata;
}

running this code运行此代码

DateTimeExtended res = new DateTimeExtended(oldDTE);

throws an InvalidOperationException with the message:抛出带有消息的InvalidOperationException

Nullable object must have a value.可为空的对象必须有一个值。

myNewDT.MyDateTime.Value - is valid and contain a regular DateTime object. myNewDT.MyDateTime.Value - 有效并包含常规DateTime对象。

What is the meaning of this message and what am I doing wrong?此消息的含义是什么,我做错了什么?

Note that oldDTE is not null .请注意, oldDTE不为null I've removed the Value from myNewDT.MyDateTime but the same exception is thrown due to a generated setter.我已经从myNewDT.MyDateTime删除了Value但由于生成的 setter 引发了相同的异常。


解决方案:

参考一: https://en.stackoom.com/question/7xHd
参考二: https://stackoom.com/question/7xHd
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部