Trigger function on deserialization

2019-04-23 08:22发布

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?

4条回答
聊天终结者
2楼-- · 2019-04-23 08:24

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;
    }
}
查看更多
可以哭但决不认输i
3楼-- · 2019-04-23 08:33

-- 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楼-- · 2019-04-23 08:35

You can implement IDeserializationCallback interface

查看更多
女痞
5楼-- · 2019-04-23 08:49

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.

查看更多
登录 后发表回答