I have various complex objects that often have collections of other complex objects. Sometimes I want to load collections only when they are needed, so I need a way to keep track of whether the collection was loaded (null / empty does not mean that it was not loaded). To do this, these complex objects are inherited from a class that supports a collection of loaded collections. Then we just need to add a function call to the setter for each collection that we want to track as follows:
public List<ObjectA> ObjectAList {
get { return _objectAList; }
set {
_objectAList = value;
PropertyLoaded("ObjectAList");
}
}
The PropertyLoaded function updates the collection, which keeps track of which collections have been loaded.
Unfortunately, these objects are used in the web service, therefore, they are serialized (de) and all setters are called, and PropertyLoaded is called when it was not really.
Ideally, I would like to use OnSerializing / OnSerialized, so the function knows if its call is called legal, however, we use the XmlSerializer so that this does not work. As far as I would like to switch to using DataContractSerializer, for various reasons I cannot do this at the moment.
Is there any other way to find out if serialization is happening or not? If not, or alternatively, is there a better way to achieve the above without the need for additional code every time you need to track a new collection?
source
share