gpt4 book ai didi

c# - DataMember IsRequired 属性与 Nullable 类型的组合是否矛盾?

转载 作者:可可西里 更新时间:2023-11-01 07:46:45 30 4
gpt4 key购买 nike

我今天在 WCF 契约(Contract)中遇到了这个:

[DataMember(IsRequired = true)]
public DateTime? LastModified { get; set; }

IsRequired = True 和可为空的 DateTime 的结果是什么?它们似乎相互矛盾。

最佳答案

如果您想用 null 初始化它并让用户设置有效日期,这是有意义的。因此在提交之前它可以验证用户输入。

这里有一个类似的矛盾可能会回答你的问题。

Interaction with IsRequired

The DataMemberAttribute attribute has an IsRequired property (the default is false). The property indicates whether a given data member must be present in the serialized data when it is being deserialized. If IsRequired is set to true, (which indicates that a value must be present) and EmitDefaultValue is set to false (indicating that the value must not be present if it is set to its default value), default values for this data member cannot be serialized because the results would be contradictory. If such a data member is set to its default value (usually null or zero) and a serialization is attempted, a SerializationException is thrown.

关于c# - DataMember IsRequired 属性与 Nullable 类型的组合是否矛盾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7766763/

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