private static void ShowResume(Resume resume) { var properties = typeof(Resume).GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (var property in properties) { if (property.PropertyType == typeof(WorkExp)) { Console.WriteLine("{0} is {1}", property.Name, (property.GetValue(resume, null) as WorkExp).CompanyName); } else if (property.PropertyType == typeof(List<WorkExp>)) { List<WorkExp> workExperiences = property.GetValue(resume, null) as List<WorkExp>; if (workExperiences != null) { Console.WriteLine("{0} : -------------------------------------", property.Name); workExperiences.ForEach((m) => { Console.WriteLine("{0} is {1}", "TimeArea", m.TimeArea); Console.WriteLine("{0} is {1}", "CompanyName", m.CompanyName); }); } } else Console.WriteLine("{0} is {1}", property.Name, property.GetValue(resume, null)); } Console.WriteLine("**************************************************************"); }
static void Main(string[] args) { Resume one = new Resume("seasun", 26, "memego"); List<WorkExp> workExps = new List<WorkExp>(); workExps.Add(new WorkExp { CompanyName = "memego", TimeArea = "2010-05-31 至今" }); workExps.Add(new WorkExp { CompanyName = "memego22222", TimeArea = "2010-05-31 22222222222 至今" }); one.WorkExperiences = workExps; one.WorkExperience = new WorkExp { CompanyName = "生力", TimeArea = "2008-2009" }; Resume two = (Resume)one.Clone(); two.Name = "seasun222"; two.WorkExperiences.Add(new WorkExp { CompanyName = "memego3333333333", TimeArea = "333333333333333 至今" }); //two.WorkExperience = new WorkExp { CompanyName = "生力22", TimeArea = "2008-2009" }; two.WorkExperience.CompanyName = "生力22222"; ShowResume(one); ShowResume(two); Console.Read(); }
static void Main(string[] args) { try { { //Resume a = new Resume("tom"); //a.SetPersonalInfo("男", 21); //a.SetWorkExperience("1998-2000", "XXX公司"); //Resume b = new Resume("tom"); //b.SetPersonalInfo("男", 21); //b.SetWorkExperience("1998-2000", "XXX公司"); //Resume c = new Resume("tom"); //c.SetPersonalInfo("男", 21); //c.SetWorkExperience("1998-2000", "XXX公司"); //a.Show(); //b.Show(); //c.Show(); } { //Resume a = new Resume("tom"); //a.SetPersonalInfo("男", 21); //a.SetWorkExperience("1998-2000", "XXX公司"); //Resume b = a; //Resume c = a; //a.Show(); //b.Show(); //c.Show(); } {//Prototype example //ConcretePrototype1 p1 = new ConcretePrototype1(1); //ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone(); //Console.WriteLine($"p1.Id={p1.Id},c1.Id={c1.Id}"); //c1.Id = 2; //Console.WriteLine($"p1.Id={p1.Id},c1.Id={c1.Id}"); } {//用原型模式实现简历 //Resume a = new Resume("tom"); //a.SetPersonalInfo("男", 21); //a.SetWorkExperience("1998-2000", "XXX公司"); //Resume b = (Resume)a.Clone(); //b.SetWorkExperience("2000-2002", "YYY公司"); //Resume c = (Resume)a.Clone(); //c.SetWorkExperience("2002-2004", "ZZZ公司"); //a.Show(); //b.Show(); //c.Show(); } {//浅拷贝 //Resume a = new Resume("tom"); //a.SetPersonalInfo("男", 21); //a.SetWorkExperience("1998-2000", "XXX公司"); //Resume b = (Resume)a.Clone(); //b.SetWorkExperience("2000-2002", "YYY公司"); //Resume c = (Resume)a.Clone(); //c.SetWorkExperience("2002-2004", "ZZZ公司"); //a.Show(); //b.Show(); //c.Show(); } {//深拷贝 Resume a = new Resume("tom"); a.SetPersonalInfo("男", 21); a.SetWorkExperience("1998-2000", "XXX公司"); Resume b = (Resume)a.Clone(); b.SetWorkExperience("2000-2006", "YYY公司"); Resume c = (Resume)a.Clone(); c.SetWorkExperience("2002-2009", "ZZZ公司"); a.Show(); b.Show(); c.Show(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }