Is there a way to exclude an object (not the class) from DataContract serialization ... RRS feed

  • Question

  • My Use case is that I have a Model (Topology) of my lab setup

    the base class is a Component ...

    The inherited classes include a Container (collection of Components) and a Platform (OS Hosted Component)

    I create a model of my lab ...

    Container(lab) => Container(Customer) => Platform(Customers Computer)

    Container(lab) => Container(Customer) => Platform(Customers Computer 1)

    Container(lab) => Container(Customer) => Platform(Customers Computer 2)

    Container(lab) => Container(Customer2) => Platform(Customer2s Computer)

    Container(lab) => Container(Customer2) => Platform(Customer2s Computer 1)

    Container(lab) => Container(Customer2) => Platform(Customer2s Computer 2)

    Each Base Class is setup thru DataContract to persist to file  ... lab->Persist(filename)

    and to be restored lab = Lab.Restore(filename)

    I want to allow a User to be able to assign a node to be Non-Persistent and to use this flag to circumvent the DataContract during the Persist call ...

    The only way I can do this currently is to pre-process the Topology and remove the nodes that have the Persist flag set to false (and all subordinate nodes if it is a container) and then call persist ...

    Is there any (other) way to prevent the data serialization of an object instantiated from a class that is set up for DataContract?

    Thursday, October 10, 2013 7:21 PM


  • Hi,

    For this you can try overriding OnSerializing() event of DataContractSerializer, and then based on your condition can skip that object from being serialized.

    Please refer to this link

    Friday, October 11, 2013 7:41 AM