title: Serializing TObjectList<T> to stream
body: <p>I'm wondering how I can perform serialization of a generic <code>TObjectList&lt;T&gt;</code> container. Basically, I want to store different objects in that list, but all objects will descend from <code>TSerializable</code>, which is defined as follows:</p>
  
  <pre><code>  TSerializable = class abstract(TObject)
    public
      { Public declarations }
      procedure LoadFromStream(const S: TStream); virtual; abstract;
      procedure SaveToStream(const S: TStream); virtual; abstract;
    end;
  </code></pre>
  
  <p>Now, let's say I have these classes defined somewhere in my app:</p>
  
  <pre><code>type
      TExampleClass = class(TSerializable)
      private
          { Private declarations }
          FIntProp: Integer;
      public
          { Public declarations }
          constructor Create();
  
          procedure LoadFromStream(const S: TStream); override;
          procedure SaveToStream(const S: TStream); override;
  
          property IntProp: Integer read FIntProp write FIntProp;
      end;
  
      TAnotherExample = class(TSerializable)
      private
          { Private declarations }
          FStringProp: String;
      public
          { Public declarations }
          constructor Create();
  
          procedure LoadFromStream(const S: TStream); override;
          procedure SaveToStream(const S: TStream); override;
  
          procedure ReverseStringProp();
  
          property StringProp: String read FStringProp write FStringProp;
      end;
  </code></pre>
  
  <p>I'm planning to store such objects in a list:</p>
  
  <pre><code>var
      MS: TMemoryStream;
      SomeList: TObjectList&lt;TSerializable&gt;;
  begin
      MS := TMemoryStream.Create();
      SomeList := TObjectList&lt;TSerializable&gt;.Create(True);
      try
          SomeList.Add(TExampleClass.Create());
          SomeList.Add(TAnotherClass.Create());
  
          TExampleClass(SomeList[0]).IntProp := 1992;
          TAnotherClass(SomeList[1]).StringProp := 'Some value';
  
          //  Here, a method to serialize the list...
          SerializeList(SomeList, MS);
  
          //  Clear the list and reset position in the stream.
          SomeList.Clear();
          MS.Seek(0, soFromBeginning);
  
          //  Unserialize the list.
          UnserializeList(SomeList, MS);
  
          //  Should display "Some value".
          Writeln(TAnotherClass(SomeList[1]).StringProp);
      finally
          SomeList.Free();
          MS.Free();
      end;
  end;
  </code></pre>
  
  <p>Now, how could I possibly serialize the whole list to stream and then re-create the list from that stream? </p>
  
  <p>What I was thinking about was:</p>
  
  <ol>
  <li>Iterate through the list.</li>
  <li>Write each object's class name to the stream first.</li>
  <li>Call <code>SaveToStream()</code> on that object.</li>
  </ol>
  
  <p>But for that approach to work, I would need to create some kind of a class register, which would be some kind of a dictionary to store known classes. It sounds like a good idea, but then I would need to call some <code>RegisterClass()</code> method to add every new class to the dictionary, and I don't like that way too much.</p>
  
  <p>Is there any other way, or should I just do it the way I proposed? </p>
  
  <p>Thanks a bunch.</p>