问题描述
我有一些这是在从类中的其他数据构造正常计算领域的一类。他们不是序列化到XML,因为对数据的其余部分的任何变化很可能会要求其重新计算。
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?
推荐答案
你所描述的的
What you are describing is [OnDeserialized]
的XmlSerializer
不支持序列化回调方法(至少不是在MS .NET;单可能有所不同)。根据您的需求,您可以尝试的DataContractSerializer
其中确实的支持序列化回调(如做一些其他的序列化)。否则,你最好的办法可能是只是有手动调用自己的公共方法。
XmlSerializer
does not support serialization callback methods (at least, not in MS .NET; mono may be different). Depending on your needs, you could try DataContractSerializer
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.
另一种方法是手动执行的IXmlSerializable
,但这是的硬盘的
Another option is to manually implement IXmlSerializable
, but this is hard.
这篇关于在反序列化触发功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!