I have a class with a number of fields which are normally calculated in the constructor from other data in the class. They are not serialized to XML because any changes to the rest of the data will likely require their recalculation.
Is there a way I can set up a function call to be triggered on deserialization?
Since an object that can be XML serialized need a public parameterless constructor, it seems you have a hole in your class design even before you hit XML serialization.
Personally I would go with lazy calculation of those fields. Store a flag inside the class whether you have calculated the fields or not, and set that field to a value signifying "out of date" when any of the properties that are used in the calculation is changed. Then, in the properties that return the calculated values, check if you need to recalculate before returning the value.
This would then work regardless of XML serialization or not.
example:
-- Edit:
Just confirmed that, as the commenter below says, the xml serialisation process doesn't hit the 'OnDeserialized'-attribute-declared method. Which is a shame.
-- Previous response:
Yes indeed, take a look here.
Specifically of interest would be the
OnDeserialized
attribute, discussed here.It may be worth noting that, depending on the serialization method used (I think) your no-arg constructor will be called. Obviously, this will happen before anything else is set. So it's probably not entirely useful.
You can implement IDeserializationCallback interface
What you are describing is
[OnDeserialized]
XmlSerializer
does not support serialization callback methods (at least, not in MS .NET; mono may be different). Depending on your needs, you could tryDataContractSerializer
which does support serialization callbacks (as do a number of other serializers). Otherwise, your best approach may be to just have your own public method that you call manually.Another option is to manually implement
IXmlSerializable
, but this is hard.