.Net学习爱好者的温馨家园

.Net互动平台

导航

通过序列化实现深拷贝

通过序列化实现深拷贝

要实现深拷贝,可以通过序列化的方式。抽象类及具体类都必须标注为可序列化的[Serializable],上面的例子加上深拷贝之后的完整程序如下:

using System;

using System.Collections;

using System.IO;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

[Serializable]

abstract class ColorPrototype

{

    public abstract ColorPrototype Clone(bool Deep);

}

[Serializable]

class ConcteteColorPrototype : ColorPrototype

{

    private int _red, _green, _blue;

    public ConcteteColorPrototype(int red, int green, int blue)

    {

        this._red = red;

        this._green = green;

        this._blue = blue;

    }

     public override ColorPrototype Clone(bool Deep)

    {

        if(Deep)

            return CreateDeepCopy();

        else

            return (ColorPrototype) this.MemberwiseClone();

    }

    //实现深拷贝

    public ColorPrototype CreateDeepCopy()

    {

        ColorPrototype colorPrototype;

        MemoryStream memoryStream = new MemoryStream();

        BinaryFormatter formatter = new BinaryFormatter();

        formatter.Serialize(memoryStream, this);

        memoryStream.Position = 0;

        colorPrototype = (ColorPrototype)formatter.Deserialize(memoryStream);

        return colorPrototype;

    }

    public ConcteteColorPrototype Create(int red,int green,int blue)

    {

        return new ConcteteColorPrototype(red,green,blue); 

    }

    public void Display(string _colorname)

    {

        Console.WriteLine("{0}'s RGB Values are: {1},{2},{3}",

            _colorname,_red, _green, _blue );

    }

}

class ColorManager

{ 

    Hashtable colors = new Hashtable();

     public ColorPrototype this[string name]

    {

        get

        {

            return (ColorPrototype)colors[name];

        }

        set

        {

            colors.Add(name,value);

        }

    }

}

class App

{

    public static void Main(string[] args)

    {

        ColorManager colormanager = new ColorManager();

        //初始化颜色

        colormanager["red"] = new ConcteteColorPrototype(255, 0, 0);

        colormanager["green"] = new ConcteteColorPrototype(0, 255, 0);

        colormanager["blue"] = new ConcteteColorPrototype(0, 0, 255);

        colormanager["angry"] = new ConcteteColorPrototype(255, 54, 0);

        colormanager["peace"] = new ConcteteColorPrototype(128, 211, 128);

        colormanager["flame"] = new ConcteteColorPrototype(211, 34, 20);

        //使用颜色

        string colorName = "red";

        ConcteteColorPrototype c1 = (ConcteteColorPrototype)colormanager[colorName].Clone(false);

        c1.Display(colorName);

        colorName = "peace";

        ConcteteColorPrototype c2 = (ConcteteColorPrototype)colormanager[colorName].Clone(true);

        c2.Display(colorName);

        colorName = "flame";

        ConcteteColorPrototype c3 = (ConcteteColorPrototype)colormanager[colorName].Clone(true);

        c3.Display(colorName);

        Console.ReadLine();

    }

}

posted on 2009-06-08 16:07  Xt Idt  阅读(822)  评论(1编辑  收藏  举报