gpt4 book ai didi

c# - 序列化复杂类型 System.Nullable

转载 作者:数据小太阳 更新时间:2023-10-29 02:59:46 25 4
gpt4 key购买 nike

我想序列化 DateTime,这样当 DateTime 为 null 时我就不会获取标签本身。

我还为上面设置了指定的 bool 值,但我的问题是 DateTime 是值类型,它永远不会为空,因此指定的 bool 值将始终为真。

我什至尝试将 DateTime 替换为 System.Nullable,但在发送请求或从 WebService 接收响应时出现序列化错误。

有什么出路吗?

最佳答案

参见 this question ,其中 Marc 给出了很好的答案。只需将 ShouldSerializeMyDateTime 方法添加到您的类中即可:

public bool ShouldSerializeMyDateTime()
{
return MyDateTime.HasValue;
}

显然,这是 XML 序列化的一个未记录的功能...您还可以使用名为 MyDateTimeSpecified

的属性

关于c# - 序列化复杂类型 System.Nullable<System.DateTime>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1237506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com