title: DataContract Serialization of List<> containing objects
body: <p>While developing a test case to understand serialization, I've run into what looks like a straighforward problem but am unable to figure out.  I want to be able to add multiple objects to a list&lt;> and then serialize that list (in this case I'm using DataContractJsonSerializer).  After creating the objects (node1 and node2), I want to add them to a List&lt;> (cn) and serialize it.  However, I get an invalid argument error when adding node1 and node2 ("cannot convert from 'JSON_test.Aspirate' to 'JSON_test.CompositeNode').  I believe it is a matter of letting the array know about the base types, but I don't know how to do that, or if in fact that is the issue (still very new to all this).  </p>
  
  <p>Thank you.</p>
  
  <pre><code>namespace JSON_test
  {
      class Program
      {
          static void Main(string[] args)
          {
              Aspirate node1 = new Aspirate(25,40);
              Dispense node2 = new Dispense(32,50);
              ObjectToSerialize cn = new ObjectToSerialize();
              cn.CompositeNode.Add (node1);
              cn.CompositeNode.Add (node2);
  
              MemoryStream stream1 = new MemoryStream();
              DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ObjectToSerialize));
              ser.WriteObject(stream1, cn.CompositeNode);
              stream1.Position = 0;
              StreamReader sr = new StreamReader(stream1);
              Console.WriteLine(sr.ReadToEnd());
              Console.ReadLine();
          }
      }
  
      [DataContract]
      public class ObjectToSerialize
      {
          private List&lt;CompositeNode&gt; compNode;
          [DataMember]
          public List&lt;CompositeNode&gt; CompositeNode
          {
              get {return this.CompositeNode;}
              set { this.compNode = value; }
          }
  
          public ObjectToSerialize()
          { 
          }
      }
  
      [DataContract]
      public class CompositeNode
      {
  
      }
  
      [DataContract]
      public class Aspirate
      {
          [DataMember]
          public string NodeName = "Aspirate";
          [DataMember]
          public double ZTravelHt;
          [DataMember]
          public double IndexHt;
  
          public Aspirate(double ZTravelHt, double IndexHt)
          {
              this.ZTravelHt = ZTravelHt;
              this.IndexHt = IndexHt;
          }
      }
  
      [DataContract]
      public class Dispense 
      {
          [DataMember]
          public string NodeName = "Dispense";
          [DataMember]
          public double ZTravelHt;
          [DataMember]
          public double IndexHt;
  
          public Dispense(double ZTravelHt, double IndexHt)
          {
              this.ZTravelHt = ZTravelHt;
              this.IndexHt = IndexHt;            
          }
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>namespace JSON_test
  {
      class Program
      {
          static void Main(string[] args)
          {
              Aspirate node1 = new Aspirate(25,40);
              Dispense node2 = new Dispense(32,50);
              ObjectToSerialize cn = new ObjectToSerialize();
              cn.CompositeNode.Add (node1);
              cn.CompositeNode.Add (node2);
  
              MemoryStream stream1 = new MemoryStream();
              DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ObjectToSerialize),
                  new Type[] {typeof (Aspirate), typeof (Dispense)});
              ser.WriteObject(stream1, cn.CompositeNode);
              stream1.Position = 0;
              StreamReader sr = new StreamReader(stream1);
              Console.WriteLine(sr.ReadToEnd());
              Console.ReadLine();
          }
      }
  
      [DataContract]
      [KnownType(typeof(Aspirate))]
      [KnownType(typeof(Dispense))]
      public class ObjectToSerialize
      {
          private List&lt;CompositeNode&gt; compNode = new List&lt;CompositeNode&gt;();
  
          [DataMember]
          public List&lt;CompositeNode&gt; CompositeNode 
          {
              get {return this.compNode;}
              set { this.compNode = value; }
          }
  
          public ObjectToSerialize()
          { 
  
          }
      }
  
      [DataContract]
      [KnownType(typeof(Aspirate))]
      [KnownType(typeof(Dispense))]
      public class CompositeNode
      {
  
      }
  
      [DataContract]
      public class Aspirate : CompositeNode
      {
          [DataMember]
          public string NodeName = "Aspirate";
          [DataMember]
          public double ZTravelHt;
          [DataMember]
          public double IndexHt;
  
          public Aspirate(double ZTravelHt, double IndexHt)
          {
              this.ZTravelHt = ZTravelHt;
              this.IndexHt = IndexHt;
          }
      }
  
      [DataContract]
      public class Dispense : CompositeNode
      {
          [DataMember]
          public string NodeName = "Dispense";
          [DataMember]
          public double ZTravelHt;
          [DataMember]
          public double IndexHt;
  
          public Dispense(double ZTravelHt, double IndexHt)
          {
              this.ZTravelHt = ZTravelHt;
              this.IndexHt = IndexHt;            
          }
      }
  }
  </code></pre>