title: How can I reuse a serialization result?
body: <p>This may be an odd situation, but here goes: I've got 2 serializable classes A and B. After an instance of A has been serialized into a byte array, an instance of B will reference that instance of A. When B's instance is serialized, I would like to take advantage of the fact that A was already serialized to avoid serializing the same thing twice. Here's some example code:</p>
  
  <pre><code>[Serializable]
  class A
  {
      // Holds some stuff
  }
  
  [Serializable]
  class B
  {
      A aMember;
      public B(A data)
      {
          aMember = data;
      }
      // Some other stuff
  }
  
  class Wubwub
  {
      void SerializeSomeThings()
      {
          var formatter = new BinaryFormatter();
          var foo = new A();
          byte[] serializedA;
          using (var firstStream = new MemoryStream())
          {
              formatter.Serialize(firstStream, foo);
              serializedA = firstStream.ToArray();
          }
  
          // Later on...
          var bar = new B(foo);
          using (var secondStream = new MemoryStream())
          {
              // How do I take advantage of serializedA?
              formatter.Serialize(secondStream, bar);
          }
      }
  }
  </code></pre>
  
  <p>I'm new to serialization so please tell me if this is impossible or doesn't make any sense. Thanks!</p>