Hello. I'm looking for a way to serialize a POCO that contains some read-only properties. In some Google and StackOverflow searches, I've seen the following suggestions:
- use DataContractSerializer; or
- use SoapFormatter or BinaryFormatter; or
- replace my readonly properties by read/write properties;
My classes are very simple, they look like:
public class MyClass
{
public int Id { get; private set; }
public string Name { get; private set; }
public MyClass(int id, string name)
{
Id = id;
Name = name;
}
}
So,
- I don't want to make my properties read/write. If they are read-only, it's because my domain model asks for read-only properties. The domain model cannot change just because of this.
- I don't want to use
DataContractSerializer
, as this would pollute my domain model with serialization-related stuff. BinaryFormatter
is not a very good option, as the result is abyte[]
, and I would like to treat it asstring
(and I don't want to deal with Encondings and alike when Deserializing my object).
What I would really like is an XmlSerializer class capable of serializing read-only properties.
Do you know of any such implementation? Or any other convenient solution?
Thanks!