Advertisement
afterlife88

Prototype

Nov 18th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace Prototype
  10. {
  11.     static class ExtensionMethods
  12.     {
  13.         public static T DeepCopy<T>(this T self)
  14.         {
  15.             if (!typeof(T).IsSerializable)
  16.                 throw new ArgumentException("type must be serializable");
  17.             if (ReferenceEquals(self, null))
  18.                 return default(T);
  19.  
  20.             var formatter = new BinaryFormatter();
  21.             using (var stream = new MemoryStream())
  22.             {
  23.                 formatter.Serialize(stream, self);
  24.  
  25.                 stream.Seek(0, SeekOrigin.Begin);
  26.                 return (T)formatter.Deserialize(stream);
  27.             }
  28.  
  29.  
  30.         }
  31.     }
  32.  
  33.     class EmployeeFactory
  34.     {
  35.         private static Contact main = new Contact() { WorkAdress = new Address() { City = "London", Street = "221b Baket Sr", Suite = 100 } };
  36.         private static Contact aux = new Contact() { WorkAdress = new Address() { City = "London", Street = "250", Suite = 100 } };
  37.  
  38.         private static Contact NewEmployee(string name, int suite, Contact prototype)
  39.         {
  40.             var result = prototype.DeepCopy();
  41.             result.Name = name;
  42.             result.WorkAdress.Suite = suite;
  43.             return result;
  44.         }
  45.         public static Contact NewMainOfficeEmployee(string name, int suite)
  46.         {
  47.             return NewEmployee(name, suite, main);
  48.         }
  49.         public static Contact NewAuxOfficeEmployee(string name, int suite)
  50.         {
  51.             return NewEmployee(name, suite, aux);
  52.         }
  53.     }
  54.     [Serializable]
  55.     public class Contact
  56.     {
  57.         public string Name;
  58.         public Address WorkAdress;
  59.  
  60.         public override string ToString()
  61.         {
  62.             return $"Name: {Name}, WorkAdress: {WorkAdress}";
  63.         }
  64.     }
  65.     [Serializable]
  66.     public class Address
  67.     {
  68.         public string Street;
  69.         public string City;
  70.         public int Suite;
  71.  
  72.         public override string ToString()
  73.         {
  74.             return $"Street: {Street}, City: {City}, Suite: {Suite}";
  75.         }
  76.     }
  77.  
  78.     class Program
  79.     {
  80.         static void Main(string[] args)
  81.         {
  82.             var john = EmployeeFactory.NewMainOfficeEmployee("John", 100);
  83.             var jill = EmployeeFactory.NewAuxOfficeEmployee("Jill", 1000);
  84.             Console.WriteLine(john);
  85.             Console.WriteLine(jill);
  86.  
  87.             Console.ReadKey();
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement