Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

C# Starter L07-Objects Cloning

1,129 views

Published on

C# Starter L07-Objects Cloning

Published in: Software
  • Be the first to comment

C# Starter L07-Objects Cloning

  1. 1. Mohammad Shaker mohammadshaker.com C# Programming Course @ZGTRShaker 2011, 2012, 2013, 2014 C# Starter L07 – Objects Cloning
  2. 2. uses Object Cloning to save the whole engine state The user can then switch back to any point in time!
  3. 3. See more of @ http://mohammadshakergtr.wordpress.com/ and @ http://www.youtube.com/watch?v=FM3v0tbdKrs
  4. 4. Cloning Objects – The Concept
  5. 5. Shallow Clone VS Deep Clone
  6. 6. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = "0001"; M2 = M1; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } }
  7. 7. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = "0001"; M2 = M1; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } } M1 Num. 0000 M2 Num. 0000 Press any key to continue...
  8. 8. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = "0001"; M2 = M1; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } } M1 Num. 0000 M2 Num. 0000 Press any key to continue...
  9. 9. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = "0001"; M2 = M1; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } } M1 Num. 0000 M2 Num. 0000 Press any key to continue...
  10. 10. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = "0001"; M2 = M1; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } } M1 Num. 0000 M2 Num. 0000 Press any key to continue...
  11. 11. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = "0001"; M2 = M1; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } } M1 Num. 0000 M2 Num. 0000 Press any key to continue...
  12. 12. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = "0001"; M2 = M1; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } } M1 Num. 0000 M2 Num. 0000 Press any key to continue...
  13. 13. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = M1.Number; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } } M1 Num. 0000 M2 Num. 0000 Press any key to continue...
  14. 14. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = M1.Number; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } } M1 Num. 0000 M2 Num. 0000 Press any key to continue...
  15. 15. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = M1.Number; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } } M1 Num. 0000 M2 Num. 0000 Press any key to continue...
  16. 16. Cloning Objects using System; using System.Reflection; namespace ConsoleApplicationCourseTest { class Mobile { public string Number { set; get; } } class MainClass { public static void Main(String[] args) { Mobile M1 = new Mobile(), M2 = new Mobile(); M1.Number = "0000"; M2.Number = M1.Number; Console.WriteLine("M1 Num. {0} nM2 Num. {1}", M1.Number, M2.Number); } } } M1 Num. 0000 M2 Num. 0000 Press any key to continue...
  17. 17. Try to Clone with IClonable interface
  18. 18. Cloning Objects • ICloneable interface • Another disadvantage of ICloneable is that the Clone method returns an object, hence every Clone call requires a cast: public interface ICloneable { object Clone(); } Person joe = new Person(); joe.Name = "Joe Smith"; Person joeClone = (Person)joe.Clone();
  19. 19. namespace ConsoleApplicationCourseTest { class MainClass { public class Person : ICloneable { public string Name; object ICloneable.Clone() { return this.Clone(); } public Person Clone() { return (Person)this.MemberwiseClone(); } } public static void Main(String[] args) { Person joe = new Person(); joe.Name = "Joe Smith"; Person bob = (Person)joe.Clone(); Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); bob.Name = "Bob PopUp"; Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); } } }
  20. 20. namespace ConsoleApplicationCourseTest { class MainClass { public class Person : ICloneable { public string Name; object ICloneable.Clone() { return this.Clone(); } public Person Clone() { return (Person)this.MemberwiseClone(); } } public static void Main(String[] args) { Person joe = new Person(); joe.Name = "Joe Smith"; Person bob = (Person)joe.Clone(); Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); bob.Name = "Bob PopUp"; Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); } } }
  21. 21. namespace ConsoleApplicationCourseTest { class MainClass { public class Person : ICloneable { public string Name; object ICloneable.Clone() { return this.Clone(); } public Person Clone() { return (Person)this.MemberwiseClone(); } } public static void Main(String[] args) { Person joe = new Person(); joe.Name = "Joe Smith"; Person bob = (Person)joe.Clone(); Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); bob.Name = "Bob PopUp"; Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); } } }
  22. 22. namespace ConsoleApplicationCourseTest { class MainClass { public class Person : ICloneable { public string Name; object ICloneable.Clone() { return this.Clone(); } public Person Clone() { return (Person)this.MemberwiseClone(); } } public static void Main(String[] args) { Person joe = new Person(); joe.Name = "Joe Smith"; Person bob = (Person)joe.Clone(); Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); bob.Name = "Bob PopUp"; Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); } } }
  23. 23. namespace ConsoleApplicationCourseTest { class MainClass { public class Person : ICloneable { public string Name; object ICloneable.Clone() { return this.Clone(); } public Person Clone() { return (Person)this.MemberwiseClone(); } } public static void Main(String[] args) { Person joe = new Person(); joe.Name = "Joe Smith"; Person bob = (Person)joe.Clone(); Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); bob.Name = "Bob PopUp"; Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); } } }
  24. 24. namespace ConsoleApplicationCourseTest { class MainClass { public class Person : ICloneable { public string Name; object ICloneable.Clone() { return this.Clone(); } public Person Clone() { return (Person)this.MemberwiseClone(); } } public static void Main(String[] args) { Person joe = new Person(); joe.Name = "Joe Smith"; Person bob = (Person)joe.Clone(); Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); bob.Name = "Bob PopUp"; Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); } } }
  25. 25. namespace ConsoleApplicationCourseTest { class MainClass { public class Person : ICloneable { public string Name; object ICloneable.Clone() { return this.Clone(); } public Person Clone() { return (Person)this.MemberwiseClone(); } } public static void Main(String[] args) { Person joe = new Person(); joe.Name = "Joe Smith"; Person bob = (Person)joe.Clone(); Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); bob.Name = "Bob PopUp"; Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); } } } joe FullName: Joe Smith bob FullName: Joe Smith joe FullName: Joe Smith bob FullName: Bob PopUp Press any key to continue...
  26. 26. namespace ConsoleApplicationCourseTest { class MainClass { public class Person : ICloneable { public string Name; object ICloneable.Clone() { return this.Clone(); } public Person Clone() { return (Person)this.MemberwiseClone(); } } public static void Main(String[] args) { Person joe = new Person(); joe.Name = "Joe Smith"; Person bob = (Person)joe.Clone(); Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); bob.Name = "Bob PopUp"; Console.WriteLine("joe FullName: {0} nbob FullName: {1}", joe.Name, bob.Name); } } } joe FullName: Joe Smith bob FullName: Joe Smith joe FullName: Joe Smith bob FullName: Bob PopUp Press any key to continue...
  27. 27. But this is all a headache!
  28. 28. The fastest way to deep clone..
  29. 29. The fastest way to deep clone
  30. 30. The fastest way to deep clone [Serialization]
  31. 31. public static class ObjectCopier { /// <summary> /// Perform a deep Copy of the object. /// </summary> /// <typeparam name="T">The type of object being copied.</typeparam> /// <param name="source">The object instance to copy.</param> /// <returns>The copied object.</returns> public static T Clone<T>(T source) { if (!typeof(T).IsSerializable) { throw new ArgumentException("The type must be serializable.", "source"); } // Don't serialize a null object, simply return the default for that object if (Object.ReferenceEquals(source, null)) { return default(T); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) { formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } } }
  32. 32. public static class ObjectCopier { /// <summary> /// Perform a deep Copy of the object. /// </summary> /// <typeparam name="T">The type of object being copied.</typeparam> /// <param name="source">The object instance to copy.</param> /// <returns>The copied object.</returns> public static T Clone<T>(T source) { if (!typeof(T).IsSerializable) { throw new ArgumentException("The type must be serializable.", "source"); } // Don't serialize a null object, simply return the default for that object if (Object.ReferenceEquals(source, null)) { return default(T); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) { formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } } }
  33. 33. public static class ObjectCopier { /// <summary> /// Perform a deep Copy of the object. /// </summary> /// <typeparam name="T">The type of object being copied.</typeparam> /// <param name="source">The object instance to copy.</param> /// <returns>The copied object.</returns> public static T Clone<T>(T source) { if (!typeof(T).IsSerializable) { throw new ArgumentException("The type must be serializable.", "source"); } // Don't serialize a null object, simply return the default for that object if (Object.ReferenceEquals(source, null)) { return default(T); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) { formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } } }
  34. 34. public static class ObjectCopier { /// <summary> /// Perform a deep Copy of the object. /// </summary> /// <typeparam name="T">The type of object being copied.</typeparam> /// <param name="source">The object instance to copy.</param> /// <returns>The copied object.</returns> public static T Clone<T>(T source) { if (!typeof(T).IsSerializable) { throw new ArgumentException("The type must be serializable.", "source"); } // Don't serialize a null object, simply return the default for that object if (Object.ReferenceEquals(source, null)) { return default(T); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) { formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } } }
  35. 35. public static class ObjectCopier { /// <summary> /// Perform a deep Copy of the object. /// </summary> /// <typeparam name="T">The type of object being copied.</typeparam> /// <param name="source">The object instance to copy.</param> /// <returns>The copied object.</returns> public static T Clone<T>(T source) { if (!typeof(T).IsSerializable) { throw new ArgumentException("The type must be serializable.", "source"); } // Don't serialize a null object, simply return the default for that object if (Object.ReferenceEquals(source, null)) { return default(T); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) { formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } } }
  36. 36. public static class ObjectCopier { /// <summary> /// Perform a deep Copy of the object. /// </summary> /// <typeparam name="T">The type of object being copied.</typeparam> /// <param name="source">The object instance to copy.</param> /// <returns>The copied object.</returns> public static T Clone<T>(T source) { if (!typeof(T).IsSerializable) { throw new ArgumentException("The type must be serializable.", "source"); } // Don't serialize a null object, simply return the default for that object if (Object.ReferenceEquals(source, null)) { return default(T); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) { formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } } }
  37. 37. End of Course Go and have some fun and come back for the next Advanced C# Course
  38. 38. Take a Look on my other courses @ http://www.slideshare.net/ZGTRZGTR Available courses to the date of this slide:
  39. 39. http://www.mohammadshaker.com mohammadshakergtr@gmail.com https://twitter.com/ZGTRShaker @ZGTRShaker https://de.linkedin.com/pub/mohammad-shaker/30/122/128/ http://www.slideshare.net/ZGTRZGTR https://www.goodreads.com/user/show/11193121-mohammad-shaker https://plus.google.com/u/0/+MohammadShaker/ https://www.youtube.com/channel/UCvJUfadMoEaZNWdagdMyCRA http://mohammadshakergtr.wordpress.com/

×