正文

序列化与深度复制例子2007-09-22 14:53:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jixian/29601.html

分享到:

using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Collections;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary; namespace _5_14{    public class DeepCopyUtility    {        public static void Main()        {            Region obj = new Region("Reg 1");            Console.WriteLine(obj);             Region copiedobj = (Region)DeepCopy(obj);            Console.WriteLine(copiedobj);             Console.WriteLine(obj == copiedobj);        }         public static object DeepCopy(object o)        {            Stream stream = null;            try            {                stream = new MemoryStream();                BinaryFormatter formatter = new BinaryFormatter();                formatter.Serialize(stream, o);                stream.Position = 0;                return formatter.Deserialize(stream);            }            finally            {                stream.Close();            }        }           }     [Serializable()]    public class Region    {        ArrayList forests;        public string name;        public Region(string name)        {            this.name = name;            forests = new ArrayList();            for (int i = 0; i < 2; i++)                forests.Add(new Forest("Forest"+i));        }        public override string ToString()        {            StringBuilder sb = new StringBuilder();            sb.Append("Region ").Append(name).Append(" has followingforests\n");            for (int i = 0; i < forests.Count; i++)            {                sb.Append(forests[i].ToString()).Append("bsn");            }            return sb.ToString();        }    }    [Serializable()]    public class Forest    {        ArrayList trees;        string name;        public Forest(string name)        {            this.name = name;            trees = new ArrayList();            for (int i = 0; i < 3; i++)                trees.Add(new Tree("Tree"+i));        }        public override string ToString()        {            StringBuilder sb = new StringBuilder();            sb.Append("\t").Append("Forset ").Append(name).Append(" has following trees\n");            for (int i = 0; i < trees.Count; i++)            {                sb.Append(trees[i].ToString()).Append("\n");            }            return sb.ToString();        }    }     [Serializable()]    public class Tree    {        ArrayList leaves;        string name;        public Tree(string name)        {            this.name = name;            leaves = new ArrayList();            for (int i = 0; i < 10; i++)                leaves.Add(new Leaf("Leaf"+i));        }        public override string ToString()        {            StringBuilder sb = new StringBuilder();            sb.Append("\t\t").Append("Tree ").Append(name).Append(" has following leaves\n");            for (int i = 0; i < leaves.Count; i++)            {                sb.Append(leaves[i].ToString()).Append("\n");            }            return sb.ToString();        }    }     [Serializable]    public class Leaf    {        string name;        public Leaf(string name)        {            this.name = name;        }        public override string ToString()        {            return name;        }    }   }

阅读(2085) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册