I've got a class that has a read-only property defined that is actually a reference to a very mutable object, and I'm wondering what the best way to handle the serialization of it is.
For example:
public class classA {
public readonly classB B = new classB();
}
public class classB {
public string Name = "Test";
}
This can be serialized to (if it wasn't for the fact the 'B' field is readonly)
<classA>
<B>
<Name>Test</Name>
</B>
</classA>
but I can't deserialize it, as the serializer would attempt to create a new instance of classB, and set the field on classA to that new object.
If I make the property not read-only, this opens up the possibility of client code changing the instance of classB that's used, which isn't acceptable (many other objects will have sunk events on that instance, so it can't just be replaced like this). Alternatively, I could have the setter simply copy the values from the object given into the instance that's already there, but this seems excessively cumbersome just to accommodate the serializer.
I considered having a separate property with a setter to handle the serialization, and setting it to [Browsable(BrowsableState.Never)], but this still exposes the ability to change the instance to anyone who gets clever with reflection.
Is there a way of telling the serializer to populate the existing object on deserialization, or am I going to have to create a custom serializer?