Trigger function on deserialization

我与影子孤独终老i 提交于 2019-12-22 04:34:12

问题


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?


回答1:


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 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.

Another option is to manually implement IXmlSerializable, but this is hard.




回答2:


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:

[XmlType("test")]
public class TestClass
{
    private int _A;
    private int? _B;

    public TestClass()
        : this(0)
    {
    }

    public TestClass(int a)
    {
        _A = a;
    }

    [XmlAttribute("a")]
    public int A
    {
        get { return _A; }
        set { _A = value; _B = null; }
    }

    [XmlIgnore]
    public int B
    {
        get { if (_B == null) Recalculate(); return _B; }
        set { _B = value; }
    }

    private void Recalculate()
    {
        _B = _A + 1;
    }
}



回答3:


-- 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.




回答4:


You can implement IDeserializationCallback interface



来源:https://stackoverflow.com/questions/2896127/trigger-function-on-deserialization

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