Hook in to OnDeserializing for XmlSerializer

柔情痞子 提交于 2019-12-12 20:40:18

问题


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 setters 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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5221124/hook-in-to-ondeserializing-for-xmlserializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!