XmlSerializer 和 OnSerializing/OnSerialized 替代品 [英] XmlSerializer and OnSerializing/OnSerialized alternatives

查看:25
本文介绍了XmlSerializer 和 OnSerializing/OnSerialized 替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有各种复杂的对象,这些对象通常包含其他复杂对象的集合.有时我只想在需要时加载集合,所以我需要一种方法来跟踪集合是否已加载(空/空并不一定意味着它尚未加载).要做到这一点,这些复杂的对象继承自一个维护加载集合集合的类.然后我们只需要为我们想要跟踪的每个集合添加对 setter 中的函数的调用,如下所示:

I have various complex objects that often have collections of other complex objects. Sometimes I only want to load the collections when they're needed so I need a way to keep track of whether a collection has been loaded (null/empty doesn't necessarily mean it hasn't been loaded). To do this, these complex objects inherit from a class that maintains a collection of loaded collections. Then we just need to add a call to a function in the setter for each collection that we want to be tracked like so:

public List<ObjectA> ObjectAList {
    get { return _objectAList; }
    set { 
        _objectAList = value; 
        PropertyLoaded("ObjectAList");
    }
}

PropertyLoaded 函数更新一个集合,以跟踪已加载的集合.

The PropertyLoaded function updates a collection that keeps track of which collections have been loaded.

不幸的是,这些对象在网络服务中被使用,因此被(反)序列化,所有的 setter 都被调用,而 PropertyLoaded 在它实际上没有被调用时被调用.

Unfortunately these objects get used in a webservice and so are (de)serialized and all setters are called and PropertyLoaded gets called when it actually hasn't been.

理想情况下,我希望能够使用 OnSerializing/OnSerialized 以便函数知道它是否被合法调用,但是我们使用 XmlSerializer 所以这不起作用.尽管我很想改用 DataContractSerializer,但出于各种原因,我目前无法这样做.

Ideally I'd like to be able to use OnSerializing/OnSerialized so the function knows if its being called legitimately however we use XmlSerializer so this doesn't work. As much as I'd like to change to using DataContractSerializer, for various reasons I can't do that at the moment.

有没有其他方法可以知道序列化是否正在发生?如果不是,或者有没有更好的方法来实现上述目标,而不必每次需要跟踪新集合时都添加额外的代码?

Is there some other way to know if serialization is happening or not? If not or alternatively is there a better way to achieve the above without having to extra code each time a new collection needs to be tracked?

推荐答案

XmlSerializer 不支持序列化回调.不过,您有一些选择.比如要选择是否序列化一个名为ObjectAList的属性,可以添加一个方法:

XmlSerializer does not support serialization callbacks. You have some options, though. For example, if you want to choose whether to serialize a property called ObjectAList, you can add a method:

public bool ShouldSerializeObjectAList () { /* logic */ }

如果你在反序列化过程中也需要知道,你可以使用:

If you need to know during deserialization too, you can use:

[XmlIgnore]
public bool ObjectAListSpecified {
    get { /* logic whether to include it in serialization */ }
    set { /* logic to apply during deserialization */ }
}

(尽管您可能会发现 - 我不能确定 - set 仅在 true 情况下被调用)

(although you might find - I can't be sure - that the set is only called for the true case)

当然,另一种选择是实现 IXmlSerializable,但这只能作为最后的手段.不好玩.

The other option, of course, is to implement IXmlSerializable, but that should only be done as a last resort. It isn't fun.

这篇关于XmlSerializer 和 OnSerializing/OnSerialized 替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆