开发者

Hook in to OnDeserializing for XmlSerializer

Otherwise than pointed out in this post I would like to act just before an XmlSerializer starts deserializing (not when finished deserializing)

Background: I have a baseClass that implements INotifyPropertyChanged. This BaseClass is stored as xml in a database and when retrieved deserialized into an object instance. The deserialization executes the set开发者_StackOverflow中文版ters of this class where my ChangeNotification takes place. On a centralised handler for the changenotification I set the status of the object and keep track of stacks for undoing changes. Apparently I don't want these to be trigerred during deserialization.

Any ideas would be very welcome!


XmlSerializer doesn't support serialization callbacks. One option is to use IXmlSerializable, but that is lots of work. In some simple cases DataContractSerializer may be a viable option; it supports the cllbacks, but it does not support as many XML scenarios (most notably: no attributes).

You may be out of luck, in which case consider a separate DTO and domain type. For example, you could deserialize into FooDto, then copy the values into a Foo, manually telling it that this is via serialization.


As commented @ Marc's solution. The most pragmatic way right now (I'm developing a proof of concept for a big chunk of complex functionality) is to default the object after it has been deserialized with a simple public void. In this method I can empty the undo stack and set the status of the object to unchanged.

If I come up with some better ideas I'll post them here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜