How to differentiate between serialized nullable data presence in file when deserializing?

How to differentiate between serialized nullable data presence in file when deserializing?



I am working with serialization using DataContract Serializer and am facing some problems with nullable data types.



I have just added a new DataMember property of Custom type into a serialized class.


[DataMember]
public CustomObject NewProperty

public get;
public set;



Now the problem is that the value of this property could be set to null and it is a valid value for the property. There is no problem when saving and the reading the object. The problem arises when I am trying to provide backwards compatibility of the saved file.


null



If I get a null value, I am not sure if the value is set to null or if it's null because it is not present it file that is being read. It's just that I have to do different processing if the file being read is old.



I have made some workaround to achieve above behavior with combination of OnDeserializing attribute hookup and setting some unique value to my property, just to check it later if it due to reading old file or not.


OnDeserializing



Something like this :


[OnDeserializing]
private void OnDeserializing(StreamingContext context)

//this.NewProperty = some unique value;


private void ProcessData()

if(this.NewProperty == unique value)

//Do something

else

//Do Something else




However, this just seems like a workaround and also I need to take care that the unique value is not getting set from somewhere else. I just need to know is there some other way to know if the null value being set is from saved file or is being set because the file is old and does not contain the property.




0



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế