title: How to serialize an interface such as IList<T>
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/464525/how-to-serialize-an-ilistt">How to serialize an IList&lt;T&gt;?</a>  </p>
  </blockquote>
  
  
  
  <p>I wish to serialize an class (let's call it <code>S</code>) that contains a property of the type <code>IList&lt;T&gt;</code> where <code>T</code> is another class which I have defined. I get an exception when I attempted to serialize an instance of class <code>S</code> to XML. This is understandable as the <code>XmlSerializer</code> doesn't know which concrete class to use. Is there a way, hopefully using attributes, to specify which concrete class to instantiate when serializing/deserializing an instance. My implementation of class <code>S</code> creates a instance of the <code>List&lt;T&gt;</code> class. Here is some code to illustrate my example:</p>
  
  <pre><code>using System;
  using System.Xml.Serialization;
  using System.IO;
  
  [Serializable]
  public class T { }
  
  [Serializable]
  public class S
  {
      public IList&lt;T&gt; ListOfTs { get; set; }
  
      public S()
      {
          ListOfTs = new List&lt;T&gt;();
      }
  }
  
  public class Program
  {
      public void Main()
      {
          S s = new S();
          s.ListOfTs.Add(new T());
          s.ListOfTs.Add(new T());
          XmlSerializer serializer = new XmlSerializer(typeof(S));
          serializer.Serialize(new StringWriter(), s);
      }
  }
  </code></pre>
  
  <p>I'm hoping there's an attribute I can place above the <code>ListOfTs</code> definition that says to the serialize, "assume an instance of <code>List&lt;T&gt;</code> when serializing/deserializing".</p>