序列化的几种策略

时间:2011-02-21 16:33:17

标签: c# .net serialization

我有一个类,可以使用二进制格式化程序进行序列化。我希望有两种序列化策略。第二种策略应该与主要策略不同,将某些特定字段排除在序列化之外。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以在该字段上使用NonSerializedAttribute,但您不希望序列化。

另请参阅this MSDN article

答案 1 :(得分:0)

创建两个memento类,每个类都包含您要序列化的信息,并添加代码以读取/输出从主类到memento类的值。

  1. 链接到维基百科中的Memento策略。
  2. 使用C#和VB.NET链接到dotfactory关于策略的文章
  3. 实施例

    创建一个具有两个依赖属性的类,“number”和“square”。设置每一个都完全定义了类。创建两个序列化类,以两种不同的方式序列化主类:

    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
            }
        }
    }