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.