Peter17
Peter17

Reputation: 3092

Several strategies for serialization

I have a class, which can be serialized using binary formatter. I want to have a two strategies for serializing. The second strategy should be different from the main one by excluding some specific fields from being serialized. How can I achieve this?

Upvotes: 0

Views: 624

Answers (2)

John Alexiou
John Alexiou

Reputation: 29244

Create two memento classes each with the information you want to serialize, and add code to read in/out the values from the main class to the memento classes.

  1. link to Memento strategy in Wikipedia.
  2. link to dotfactory article on the strategy with C# and VB.NET

Example

Crate a class with two dependent properties, "number" and "square". Setting each one completely defines the class. Create two seriazation classes to serialize the main class two different ways:

class Data // The main class to store data in
{
    int x;
    public Data() { this.x = 0; }
    public int Number
    {
        get { return x; }
        set { x = value; }
    }
    public int Square
    {
        get { return x * x; }
        set { x = (int)Math.Sqrt(value); }
    }
    public void FromNumberStore(NumberMemento mem)
    {
        this.Number = mem.Number;
    }
    public void FromSqureStore(SquareMemento mem)
    {
        this.Square = mem.Square;
    }
}

[Serializable]
class NumberMemento // memento #1
{
    int x;
    public NumberMemento() { x = 0; }
    public NumberMemento(Data data)
    {
        this.x = data.Number;
    }
    public int Number
    {
        get { return x; }
        set { x = value; }
    }
}
[Serializable]
class SquareMemento // memento #2
{
    int x2;
    public SquareMemento() { x2 = 0; }
    public SquareMemento(Data data)
    {
        this.x2 = data.Square;
    }
    public int Square
    {
        get { return x2; }
        set { x2 = value; }
    }
}
class Program // Sample code to check all around serialization.
{
    static void Main(string[] args)
    {
        Data store = new Data();
        store.Number = 9;
        {
            // Write and read based on number
            NumberMemento mem1 = new NumberMemento(store);
            BinaryFormatter bf1 = new BinaryFormatter();
            FileStream fs = new FileStream("numstore.dat", FileMode.Create);
            bf1.Serialize(fs, mem1);
            fs.Close();
            // clear data and deserialize
            store.Number = 0;
            fs = new FileStream("numstore.dat", FileMode.Open);
            mem1 = bf1.Deserialize(fs) as NumberMemento;
            fs.Close();
            store.FromNumberStore(mem1);
            // check store.Number == 9
        }
        {
            // Write and read based on square
            SquareMemento mem2 = new SquareMemento(store);
            BinaryFormatter bf2 = new BinaryFormatter();
            FileStream fs = new FileStream("sqrstore.dat", FileMode.Create);
            bf2.Serialize(fs, mem2);
            fs.Close();
            // clear data and deserialize
            store.Number = 0;
            fs = new FileStream("sqrstore.dat", FileMode.Open);
            mem2 = bf2.Deserialize(fs) as SquareMemento;
            fs.Close();
            store.FromSqureStore(mem2);
            // check store.Number == 9
        }
    }
}

Upvotes: 0

Shekhar_Pro
Shekhar_Pro

Reputation: 18430

You can use NonSerializedAttribute on that field which you don't want to Serialize.

Also look at this MSDN article on this.

Upvotes: 1

Related Questions