|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
.NET Serialization QuestHi All,
I have a question concerning serialization in .NET. If I serialize an object to a database and then I change the object in code (i.e. add a property, rename a property, delete a property) will my object deserialize correctly? Will it throw an exception - or will it deserialize whatever it can? If serialization is going to throw an error, is there a way to do a "best effort" deserialization or handle changed objects? Thanks! Spam Catcher wrote:
Show quoteHide quote > Hi All, I have another question: how are you serializing your objects to the> > I have a question concerning serialization in .NET. > > If I serialize an object to a database and then I change the object in code > (i.e. add a property, rename a property, delete a property) will my object > deserialize correctly? > > Will it throw an exception - or will it deserialize whatever it can? > > If serialization is going to throw an error, is there a way to do a "best > effort" deserialization or handle changed objects? > > Thanks! database? I just started work on a similar problem and I haven't even gotten the objects to the database yet. If I can get that far, I'll let you know how well they come back out of the database. lord.zol***@gmail.com wrote in news:1163520931.928255.279730
@i42g2000cwa.googlegroups.com: > I have another question: how are you serializing your objects to the Here's my code:> database? I just started work on a similar problem and I haven't even > gotten the objects to the database yet. If I can get that far, I'll let > you know how well they come back out of the database. Public Shared Function Serialize(ByVal instance As Object) As String Dim _Serializer As New SoapFormatter Using _MS As New MemoryStream Try _Serializer.Serialize(_MS, instance) Return System.Text.Encoding.ASCII.GetString(_MS.ToArray) Catch ex As Exception Return "" End Try End Using End Function Public Shared Function Deserialize(ByVal XML As String) As Object Dim _Serializer As New SoapFormatter Using _MS As New MemoryStream (System.Text.Encoding.ASCII.GetBytes(XML)) Try Return _Serializer.Deserialize(_MS) Catch ex As Exception Return Nothing End Try End Using End Function Spam Catcher wrote:
Show quoteHide quote > Hi All, I have another question: how are you serializing your objects to the> > I have a question concerning serialization in .NET. > > If I serialize an object to a database and then I change the object in code > (i.e. add a property, rename a property, delete a property) will my object > deserialize correctly? > > Will it throw an exception - or will it deserialize whatever it can? > > If serialization is going to throw an error, is there a way to do a "best > effort" deserialization or handle changed objects? > > Thanks! database? I just started work on a similar problem and I haven't even gotten the objects to the database yet. If I can get that far, I'll let you know how well they come back out of the database. That depends on how you do it. I'm assuming you're serializing to XML
using an IXmlSerializable implementation. In that case, you're pretty much in charge of the serialization and therefore the behavior under fringe cases. If this is the case, the story goes something like this: 1) You have your object. You create an XmlSerializer that'll spit out either a file or a stream containing your XML upon request. You can either tie the stream to blob access or turn the stream in to a string and write it parametrically. 2) When you get your object back, you get the same, either a string or a stream. You create an XmlSerializer and pass it your data as a stream. 3) It'll call your null constructor then the implemented ReadXml() method. Whatever you've chosen to write in the method will work as written. The implication of this is that you can make your ReadXml() method as robust to change as you want. If the version 1.0 object has members A and B and version 2.0 adds C, everything *can* work out if ReadXml() is overridden correctly and C is nullable or defaultable. As far as properties and such, you're on your own. Serialization couldn't care less about anything outside the IXmlSerializable implementation. Stephan Spam Catcher wrote: Show quoteHide quote > Hi All, > > I have a question concerning serialization in .NET. > > If I serialize an object to a database and then I change the object in code > (i.e. add a property, rename a property, delete a property) will my object > deserialize correctly? > > Will it throw an exception - or will it deserialize whatever it can? > > If serialization is going to throw an error, is there a way to do a "best > effort" deserialization or handle changed objects? > > Thanks! First, you need to separate xml-serialization and
binary/SOAP-serialization; they work differently. When adding fields/properties: For binary, you can look at [OptionalField], or provide your own "best efforst" implementation via ISerializable without too much trouble. For xml, you can look at [DefaultValue], which has the effect of making a property optional. You could also implement IXmlSerializable, but IIRC it is a pig (providing the schema, mainly). When deleting: you tell me. A simple test app should take about 3 minutes to write... Marc
Application performance decrease after deleting rows
Reading value of a bit? Check for Duplicate Appointment Times Sound with New System.Media.SoundPlayer() Hiding Default Constructor in VB Express 2005 (Public Not Creatable in VB6) Directory.Getfiles search pattern New SQL Express 2005 ? Cannot open a database from a previous version of your application compare/check character against an array of chars - best practice? Get Product Keys |
|||||||||||||||||||||||