c# - Problems deserializing class after newly added member -


i have simple "serializable" class defined follows:

[serializable] public class layoutdetails {            public bool isrefreshenabled { get; set; }      public string gridsettings { get; set; }      public list<string> groupbypropertynames { get; set; } } 

the class has been serialized using following routine:

using (var memorystream = new memorystream()) {    var dc = new datacontractserializer(obj.gettype());    dc.writeobject(memorystream, obj);     memorystream.flush();     memorystream.position = 0;    streamreader reader = new streamreader(memorystream);    serializedobject = reader.readtoend(); } 

i trying deserialize using following routine:

   var bytes = new utf8encoding().getbytes(serializedobject);    using (var stream = new memorystream(bytes ))    {       var serializer = new datacontractserializer(type);       var reader = xmldictionaryreader.createtextreader(stream, xmldictionaryreaderquotas.max);       obj = serializer.readobject(reader, true);    } 

this worked great; however, now, want add new property class (public int? offset { get; set; }). when add property class , try deserialize serialized instances (without property), exception:

error in line 1 position 148. 'endelement' 'layoutdetails' namespace 'http://schemas.datacontract.org/2004/07/wpfapplication1' not expected. expecting element '_x003c_offset_x003e_k__backingfield'. 

my first thought implement iserializable , use deserialziation ctor, tried this, can't find members name - , i've tried several combinations...

public layoutdetails(serializationinfo info, streamingcontext context) {    this.isrefreshenabled = (bool)info.getvalue("_x003c_isrefreshenabled_x003e_k__backingfield", typeof(bool));    this.isrefreshenabled = (bool)info.getvalue("_x003c_isrefreshenabled_x003e_k_", typeof(bool));    this.isrefreshenabled = (bool)info.getvalue("_x003c_isrefreshenabled_x003e", typeof(bool));    this.isrefreshenabled = (bool)info.getvalue("x003c_isrefreshenabled_x003e", typeof(bool));    this.isrefreshenabled = (bool)info.getvalue("isrefreshenabled", typeof(bool)); } 

using datacontractserializer, how can add new member class without breaking deserialization on "old" instances?

you can use backing fields new properties, , add attribute optionalfield:

[serializable] public class layoutdetails {            [optionalfield]     private int? offset;      public bool isrefreshenabled { get; set; }      public string gridsettings { get; set; }      public list<string> groupbypropertynames { get; set; }      public int? offset     {         { return offset; }         set { offset = value; }     } } 

if want setup default value, can add method class attribute ondeserializing:

[ondeserializing] private void setoffsetdefault(streamingcontext sc) {     offset = 123; } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -