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

  namespace PrototypePattern {
    // Prototype Pattern        Judith Bishop  Dec 2006
    // Serialization is used for the deep copy option
     
    [Serializable()]
    public abstract class IPrototype <T> {
      public T Clone() {
          return (T) this.MemberwiseClone(); // Shallow copy
      }
      
      public T DeepCopy() { // Deep Copy
          MemoryStream stream = new MemoryStream();
          BinaryFormatter formatter = new BinaryFormatter();
          formatter.Serialize(stream, this);
          stream.Seek(0, SeekOrigin.Begin);
          T copy = (T) formatter.Deserialize(stream);
          stream.Close();
        return copy;
      }
    }
  }

相关文章:

  • 2021-11-02
  • 2021-06-30
  • 2021-08-21
  • 2021-08-16
  • 2022-01-16
  • 2021-11-12
  • 2021-10-12
猜你喜欢
  • 2021-10-14
  • 2022-01-12
  • 2021-12-25
  • 2021-07-06
  • 2021-11-12
  • 2021-06-13
  • 2021-12-22
相关资源
相似解决方案