Removing deserialization of a new version of an object from an old version of an object

Suppose I had this class:

[Serializable] public class SomeClass { public SomeClass() {//init} public string SomeString {get;set;} } 

This class gets serialized when the application closes and receives deserialization in the next run.

Then I built it and released the application, and now the class has changed:

 [Serializable] public class SomeClass { public SomeClass() {//init} public string SomeString {get;set;} public int SomeInt {get;set;} } 

Is there a way to set the default property when deserializing if it is not found in the old serialized object?

One of the ways I thought was to save the old version of the class, and then check the version that was serialized, and then loop the properties of the old object and set them in the new object, but that doesn't make sense to me, any other solution that has meaning?

+6
source share
2 answers

You can mark fields with attribute

 [OptionalField()] 

as described in Version Allowed Serialization

The class will look like this:

 [Serializable()] public class SomeClass { public SomeClass() {//init} public string SomeString { get; set; } [OptionalField(VersionAdded = 2)] public int SomeInt { get; set; } [OnDeserialized()] private void SetValuesOnDeserialized(StreamingContext context) { this.SomeInt = 10; } } 
+4
source

What I would do is base SomeInt in the field, where the field has a default value. IE

 public class SomeClass { public SomeClass() { } int someInt = 10; public string SomeString { get; set; } public int SomeInt { get { return someInt; } set { someInt = value; } } } 

Then, when the serializer deserializes the object, if SomeInt is not specified, the default value is still set.

EDIT: Update

Added sample application using an XML serializer. Now to switch the class type, just uncomment the #define serialize on line 2.

 //uncomment for serialization //#define serialize using System; using System.IO; using System.Xml.Serialization; namespace TestSerializer { class Program { static void Main(string[] args) { #if serialize SomeClass some = new SomeClass(); some.SomeString = "abc"; XmlSerializer serializer = new XmlSerializer(some.GetType()); using (StringWriter writer = new StringWriter()) { serializer.Serialize(writer, some); File.WriteAllText("D:\\test.xml", writer.ToString()); } #else XmlSerializer serializer = new XmlSerializer(typeof(SomeClass)); using (StringReader reader = new StringReader(File.ReadAllText("D:\\test.xml"))) { var o = serializer.Deserialize(reader) as SomeClass; if (o != null) Console.WriteLine(o.SomeInt); } Console.ReadKey(); #endif } } #if serialize [Serializable] public class SomeClass { public SomeClass() { } public string SomeString { get; set; } } #else [Serializable] public class SomeClass { public SomeClass() { } private int someInt = 10; public string SomeString { get; set; } public int SomeInt { get { return someInt; } set { someInt = value; } } } #endif } 
+2
source

Source: https://habr.com/ru/post/957154/


All Articles