Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the DataMember IsRequired attribute in combination with a Nullable type contradictory?

I came across this today in a WCF contract:

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

What are the consequences of IsRequired = True and a nullable DateTime? They appear to be contradictory to each other.

like image 920
Rebecca Avatar asked Oct 14 '11 11:10

Rebecca


People also ask

What does DataMember attribute do?

Gets or sets a value that specifies whether to serialize the default value for a field or property being serialized.

What is Datacontract and DataMember?

A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.

What is DataMember in asp net?

The DataMember property sets or returns a string value that contains the name of the data member that will be retrieved from the object referenced by the DataSource property. The name is not case sensitive. This property is used to create data-bound controls with the Data Environment in Visual Basic 6.

Which attribute specifies that a public property of a class is capable of being serialized?

You must specify [DataMember] attribute on the property or the field of your Data Contract class to identify it as a Data Member. DataContractSerializer will serialize only those members, which are annotated by [DataMemeber] attribute.


2 Answers

It can make sense if you want to initialize it with null and let user to set a valid date. So before submitting it can validate user input.

Here is a similar contradictory that may answer your question.

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.

like image 126
CharithJ Avatar answered Sep 29 '22 16:09

CharithJ


A guess: you MUST have a node for 'LastModified' (=required) but the contents can be empty (=value is null).

like image 29
Hans Kesting Avatar answered Sep 29 '22 17:09

Hans Kesting