Hello,
I'm having trouble serializing an immutable instance with the DataContractSerializer, since the properties on the class I'm serializing is missing setters. The problem is that I only want to serialize the instance (just so it can be written to a log), and I will never need to deserialize it. Is there a way to bypass this behavior?
The class I'm trying to serialize:
[DataContract]
public class Person
{
private readonly string _name;
[DataMember]
public string Name
{
get { return _name; }
}
public Person(string name)
{
_name = name;
}
}
The code used for serializing the class:
public string Serialize()
{
var serializer = new DataContractSerializer(typeof(Person));
StringBuilder stringBuilder = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(stringBuilder)) {
serializer.WriteObject(writer, this);
}
return stringBuilder.ToString();
}