Esempio n. 1
0
        /// <summary>
        /// 深层克隆.
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            // 先 浅层克隆.
            MessageLog result = this.MemberwiseClone() as MessageLog;

            // 然后额外设置 需要 深层次克隆的属性.
            result.Message = this.Message.Clone() as Message;

            // 返回.
            return(result);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Message mess = new Message()
            {
                From ="张三",
                To = "李四",
                Data = "Hello!"
            };

            MessageLog log = new MessageLog()
            {
                LogID = 1,
                Message = mess
            };

            Console.WriteLine("克隆前......");
            Console.WriteLine(log);
            Console.WriteLine();

            Console.WriteLine("浅层克隆后......");
            MessageLog log2 = log.SimpleClone();
            Console.WriteLine(log2);

            Console.WriteLine("修改克隆后数据,检查是否影响原始数据!");
            log2.Message.Data = "Hello V2.0";

            Console.WriteLine("原始数据={0}", log);
            Console.WriteLine("克隆数据={0}", log2);

            Console.WriteLine();

            Console.WriteLine("深层克隆后......");
            log2 = log.Clone() as MessageLog;
            Console.WriteLine(log2);

            Console.WriteLine("修改克隆后数据,检查是否影响原始数据!");
            log2.Message.Data = "Hello V3.0";

            Console.WriteLine("原始数据={0}", log);
            Console.WriteLine("克隆数据={0}", log2);

            Console.ReadLine();
        }