c# - implementing ISerializable for version control -


consider following pod type of class:

public class price { public decimal offerprice { get; set; } } 

objects class retrieved server let's decorate serializable

[serializable] public class price { public decimal offerprice { get; set; } } 

now there's 2 clients on different machines retrieving these objects. wont send prices. copy of price's assembly.

now class expanded bonusprice.

[serializable] public class price {    public decimal offerprice { get; set; }    public decimal bonusprice { get; set; }  } 

the new assembly deployed server, 1 of clients not other. older versioned client crash when (de)serializing price objects.

the client old version not need bonusprice field nice keeps working when there's version difference. therefore i'm thinking of implementing iserializable beginning first , second version like:

// version 1.0 [serializable] public class price : iserializable {   protected price(serializationinfo info, streamingcontext context) {     offerprice = info.getdecimal("op");   }    [securitypermission(securityaction.demand, serializationformatter = true)]   public virtual void getobjectdata(serializationinfo info, streamingcontext context) {     info.addvalue("op", offerprice);   } }  // version 2.0 [serializable] public class price : iserializable {   protected price(serializationinfo info, streamingcontext context) {     offerprice = info.getdecimal("op");     bonusprice = info.getdecimal("bp");   }    [securitypermission(securityaction.demand, serializationformatter = true)]   public virtual void getobjectdata(serializationinfo info, streamingcontext context) {     info.addvalue("op", offerprice);     info.addvalue("bp", bonusprice);   } } 

so when 1 client not updated version 2 still keep on deserializing offerprice , not crash. when it's updated @ point use bonusprice automatically.

my question: implementing iserializable way go version control when reading objects? how these problems solved?

you can use optionalfieldattribute control versioning of binaryformatter , soapformatter.

the optionalfieldattribute has versionadded property. in version 2.0 of .net framework, not used. however, important set property correctly ensure type compatible future serialization engines.

the property indicates version of type given field has been added. should incremented 1 (starting @ 2) every time type modified

also there other ways serialization callbacks, serializationbinder, iserializable etc..

refer version tolerant serialization more info.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -