In some cases, we need shallow copy to solve the problem, because the instance we copy still references the original initial object. But sometimes, this is not enough, because the instance we copy needs to modify and adjust the local value of the reference type, and ensure that it does not affect the initial object!
This requires deep copying!
The requirements are as follows:
First, let’s take a look at why shallow copy cannot meet our requirements: we want to copy the resume, and the copied version is only inconsistent with the company name in the job intention of the template resume.
Our first version of the code is like this:
ResumeModel1.0 version
public class ResumeInfo1:ICloneable { public ResumeInfo1(string name, string telephone, EducationInfo educationInfo,WantedJob1 job) { this._name = name; this._telephone = telephone; this._educationInfo = educationInfo; this._wantedJob = job; } private string _name; public string name { get { return this._name; } } private string _telephone; public string telephone { get { return _telephone; } } private EducationInfo _educationInfo; public EducationInfo educationInfo { get { return _educationInfo; } } private WantedJob1 _wantedJob; public WantedJob1 WantedJob { get { return _wantedJob; } } public object Clone() { return this.MemberwiseClone(); } }
The nested subclass education background object EducationInfo
public class EducationInfo { public string schoolName { get; set; } public DateTime enrollTime { get; set; } public DateTime leaveTime { get; set; } }
There is also a nested object WantedJob1:
public class WantedJob1 { public string companyName { get; set; } public double eanrings { get; set; } }
We use it on the client:
EducationInfo educationInfo = new EducationInfo(); WantedJob1 wantedJob = new WantedJob1(); ResumeInfo1 templateResume = new ResumeInfo1("qaz", "18810002000", educationInfo, wantedJob); educationInfo.enrollTime = new DateTime(2010, 7, 1); educationInfo.leaveTime = new DateTime(2015, 1, 1); educationInfo.schoolName = "wsx"; wantedJob1.companyName = "edc"; wantedJob1.eanrings = 1000; //假定各个简历版本,仅仅companyName属性值不同。 var res1 = templateResume.Clone(); (res1 as ResumeInfo1).WantedJob.companyName = "baidu"; var res2 = templateResume.Clone(); (res1 as ResumeInfo1).WantedJob.companyName = "ali";
But the company names we get are all "ali"
This is a typical shallow copy!
Cannot meet the requirements of distinguishing the company name, continue to modify and change to deep copy:
public class ResumeInfo2:ICloneable { public ResumeInfo2(string name, string telephone, EducationInfo educationInfo,WantedJob2 job) { this._name = name; this._telephone = telephone; this._educationInfo = educationInfo; this._wantedJob = job; } // private void cloneJob(WantedJob2 job) { this._wantedJob = job.Clone() as WantedJob2; } private string _name; public string name { get { return this._name; } } private string _telephone; public string telephone { get { return _telephone; } } private EducationInfo _educationInfo; public EducationInfo educationInfo { get { return _educationInfo; } } private WantedJob2 _wantedJob; public WantedJob2 WantedJob { get { return _wantedJob; } } public object Clone() { cloneJob(this._wantedJob); return new ResumeInfo2(_name,_telephone,_educationInfo,_wantedJob); } }
Job-seeking intention object 2.0:
//WantedJob2 实现接口 public class WantedJob2:ICloneable { public string companyName { get; set; } public double eanrings { get; set; } public object Clone() { return this.MemberwiseClone(); } }
Client call:
//此处我们需要对WantedJob做深复制处理。 EducationInfo educationInfo = new EducationInfo(); WantedJob2 wantedJob = new WantedJob2(); ResumeInfo2 templateResume = new ResumeInfo2("qaz", "18810002000", educationInfo, wantedJob); educationInfo.enrollTime = new DateTime(2010, 7, 1); educationInfo.leaveTime = new DateTime(2015, 1, 1); educationInfo.schoolName = "wsx"; wantedJob.companyName = "edc"; wantedJob.eanrings = 1000; //假定各个简历版本,仅仅companyName属性值不同。 var res1 = templateResume.Clone(); (res1 as ResumeInfo2).WantedJob.companyName = "baidu"; var res2 = templateResume.Clone(); (res2 as ResumeInfo2).WantedJob.companyName = "ali";
Get different company names! Deep copy successful!
The above is the detailed content of .NET Framework-detailed sample code of how to change Clone from shallow to deep. For more information, please follow other related articles on the PHP Chinese website!