title: How do I create List<type> for runtime type?
body: <p>I have some code like this, I don't know what type is until runtime.</p>
  
  <pre><code>Type type;
  </code></pre>
  
  <p>And I need a list defined as:</p>
  
  <pre><code>List&lt;type&gt;
  </code></pre>
  
  <p>I've tried</p>
  
  <pre><code>var listType = typeof(List&lt;&gt;).MakeGenericType(new []{type});
  
  IList list = Activator.CreateInstance(listType));
  </code></pre>
  
  <p>But it does not work well because I need to pass this list to XmlSerializer.Serialize method.  <strong>It only output a correct result when this list is defined exactly List</strong></p>
  
  <p>for example:
  When I pass in </p>
  
  <p><code>List&lt;String&gt; list = new List&lt;String&gt;()</code></p>
  
  <p>It output</p>
  
  <pre><code>&lt;ArrayOfString&gt;...&lt;/ArrayOfString&gt;
  </code></pre>
  
  <p>When I pass in </p>
  
  <p><code>IList list = new List&lt;String&gt;()</code></p>
  
  <p>It will output</p>
  
  <pre><code>&lt;ArrayOfAnyType&gt;...&lt;/ArrayOfAnyType&gt;
  </code></pre>
  
  <p>which is not what I want.</p>
  
  <p>How can I define a List when I don't know what type is at compile time?</p>