Serialization - Xml - Readonly Properties
I was recently working on a project where I needed to serialize objects with properties marked as readonly (can only be set inside a constructor). The objects are part of a WCF RESTful service security infrastructure so I wasn’t about to change their design just because I needed to serialize them, there is no need too! Anyway baring that in mind the lions share of the serialization duties in this project rely on POX (Plain Old Xml) serialisation and XmlFormatter’s etc. So … if you have an object like this:
You’re almost certainly going to run into Xml serialization issues unless you use a BinaryFormatter object and Serialize/Deserialize that way. In the next snippet you’ll see two extension methods written in C# that perform these tasks and extend both the Object and String types.
This first method takes your CLR object as input and produces a base 64 encoded string object from it ready for storing somewhere.
This next method takes in the string produced from the previous method and turns it back into a a CLR object of the type specified by T.
If you want to have a look at this in action I have produced a small demonstration solution which you can download and have a look at, pinch the code from etc …
Continuous Integration - TeamCity »
