c# - Deserialization error using OptionalField -


after add new optional field class, serialized instances of class no longer deserializable.

suppose have saved instances of myclass using binaryformatter:

[serializable] public class myclass {     public mytype a; } 

after, second revision of myclass:

[serializable] public class myclass {     public mytype a;      [optionalfield(versionadded = 2)]     public mytype newfield; } 

now older objects no longer deserializable. stack trace obtain when trying deserialize them following (the profile .net 4.0):

system.argumentnullexception: value cannot null.     parameter name: type        @ system.reflection.assembly.getassembly(type type)        @ system.runtime.serialization.formatters.binary.binaryconverter.getparserbinarytypeinfo(type type, object& typeinformation)        @ system.runtime.serialization.formatters.binary.objectmap..ctor(string objectname, type objecttype, string[] membernames, objectreader objectreader, int32 objectid, binaryassemblyinfo assemblyinfo)        @ system.runtime.serialization.formatters.binary.__binaryparser.readobjectwithmap(binaryobjectwithmap record)        @ system.runtime.serialization.formatters.binary.__binaryparser.run()        @ system.runtime.serialization.formatters.binary.objectreader.deserialize(headerhandler handler, __binaryparser serparser, boolean fcheck, boolean iscrossappdomain, imethodcallmessage methodcallmessage)        @ system.runtime.serialization.formatters.binary.binaryformatter.deserialize(stream serializationstream, headerhandler handler, boolean fcheck, boolean iscrossappdomain, imethodcallmessage methodcallmessage)        @ system.runtime.serialization.formatters.binary.binaryformatter.deserialize(stream serializationstream, headerhandler handler, boolean fcheck, imethodcallmessage methodcallmessage)        @ system.runtime.serialization.formatters.binary.binaryformatter.deserialize(stream serializationstream, headerhandler handler, boolean fcheck) 

i cannot find stack trace on internet or similar stack traces. note same files readable running software mono ;-) . because of suppose problem related .net bug.

suppose have following class types.

[serializable] public class myclass {     public mytype a; }  [serializable] public class mytype {     public string name { get; set; } } 

let's serialize instance of myclass file.

using (var stream = new filestream(@"c:\temp\test.dat", filemode.create, fileaccess.write)) {     var formatter = new binaryformatter();     formatter.serialize(stream, new myclass { = new mytype { name = "christophe" } }); } 

now deserialize instance of myclass.

using (var stream = new filestream(@"c:\temp\test.dat", filemode.open, fileaccess.read)) {     var formatter = new binaryformatter();     var myinstance = (myclass) formatter.deserialize(stream); } 

no problem. works fine. let's add new field myclass type. suggest use properties instead.

[serializable] public class myclass {     public mytype a;      [optionalfield]     public mytype b; } 

deserialization still works fine. in case missing data b ignored , set null.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -