惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
P
Privacy International News Feed
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 叶小钗
F
Fortinet All Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 最新话题
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
Recent Announcements
Recent Announcements
Recent Commits to openclaw:main
Recent Commits to openclaw:main
PCI Perspectives
PCI Perspectives
Cloudbric
Cloudbric
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
IT之家
IT之家
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
J
Java Code Geeks
M
MIT News - Artificial intelligence
Cisco Talos Blog
Cisco Talos Blog
V2EX - 技术
V2EX - 技术
Webroot Blog
Webroot Blog
Microsoft Security Blog
Microsoft Security Blog
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
G
Google Developers Blog
W
WeLiveSecurity
罗磊的独立博客
P
Privacy & Cybersecurity Law Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
T
Tailwind CSS Blog
V
Visual Studio Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Secure Thoughts
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
Blog — PlanetScale
Blog — PlanetScale
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed

博客园 - -Enchant

Linux上搭建Asp.net MVC3环境(CentOS + Nginx + Mono) 《单例模式》你需要注意的问题 系统框架整理 Extjs prompt 显示密码框 Python网页抓取、模拟登录 单点登录(SSO)的一点思考 Jquery以JSON方式调用WebService WCF初探 关于抓取百度搜索内容 iPhone开发环境搭建(备忘) SMTP/POP3命令简介(转) C/S模式下 简单的定时任务功能 Asp.net MVC2学习笔记索引 Oracle 调优 Oracle常见错误 @OutputCache指令参数 关于ACL权限控制【ASP.NET MVC2】 c#递归生成XML ASP.NET MVC2 Ajax返回JSON
引用类型的对象复制(浅复制和深复制)
-Enchant · 2010-03-01 · via 博客园 - -Enchant

引用类型的对象复制,分为浅复制和深复制。。。

浅复制就简单的把地址复制给另一个对象,其中有一个对象发生变化,2个都有变化。

实现方式,实现 ICloneable 接口,调用 MemberwiseClone()方法就可以了。

深复制就完全产生一个新的对象,二者属性一样,但没有任何关联。。

实现方式,可以重新 new 一个对象,也可以通过序列化、反序列化来实现。。

代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace H.DBF
{
    /// 

<summary>
    /// Model对象基类
    /// 
</summary>
    [Serializable]
    public abstract class ObjBase:ICloneable
    {
        #region ICloneable 成员
        /// 
<summary>
        /// 浅复制
        /// 
</summary>
        /// 
<returns></returns>
        public object Clone()
        {
            return this.MemberwiseClone();
        }

        /// 

<summary>
        /// 深度拷贝,使用序列化方式,在数据量大时速度很慢!
        /// 
</summary>
        /// 
<returns></returns>
        public object DeepClone()
        {
            //使用 序列化/反序列化,在数据量大时相当慢!
            object obj;
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter(null, new System.Runtime.Serialization.StreamingContext());

                bf.Serialize(ms, this);
                ms.Seek(0, SeekOrigin.Begin);

                obj = bf.Deserialize(ms);
                ms.Close();

                return obj;
            }
            //ObjBase obj = (ObjBase) System.Activator.CreateInstance(this.GetType());
            //obj.TableName = this.TableName;
            //obj.Columns = this.Columns;

            //return obj;
        }
        #endregion
    }
}