Here's my need.
When certain data is present in a class (collection nuts) , when serializing that class, other data (collection bolts) should not be serialized. If that data is not present (no nuts), the other data (bolts) should be serialized with the parent class.
When deserializing, if bolts are found, go ahead and consume them but if nuts are found, use the nuts to find the bolts from some other location.
Primarily, this is to maintain backward compatiblity with a previous version of a serializable class that I have been using. I can see this coming up again though.
public class MarsRover
{
public nut[] Nuts; [NonSerialized][XmlIgnore]public bolt[] Bolts; public void SerializeToXml() { // Serialization - serilizes to some stadard file locationXmlSerializer xmlsSlzr = new XmlSerializer( typeof( MarsRover) );TextWriter txWtr = new StreamWriter( getXmlConfigFilePath() ); /// here's the thing /// if there are no nuts, serialize the bolts. //of course, I can manually serialize the bolts and insert them into the resultant doc, //and vice versa, but it would be much easier to pass that instruction to the serializer... xmlsSlzr.Serialize( txWtr, this );txWtr.Close(); }
public nut[] Nuts;
[NonSerialized][XmlIgnore]public
public
/// here's the thing /// if there are no nuts, serialize the bolts.
//of course, I can manually serialize the bolts and insert them into the resultant doc, //and vice versa, but it would be much easier to pass that instruction to the serializer...
xmlsSlzr.Serialize( txWtr, this );txWtr.Close();
}