title: create List of Type using Type variable at runtime
body: <p>I'm trying to simplify my methods in creating several variables in a function which is repeated for different types and different actions, eventually I will migrate to a single function that can handle multiple types but I'm doing it in stages. I would like to be able to make a list of objects in their types instead of List without having to copy/paste the object type everywhere. I had hoped to accomplish this by something really simple like</p>
  
  <pre><code>Type t = typeof(LinkAddy);
  var x = List&lt;t&gt;();
  </code></pre>
  
  <p>but this doesn't compile.</p>
  
  <p>Then I tried a different solution provided by the following answer: <a href="https://stackoverflow.com/questions/6629941/storing-variable-type-and-using-it-to-create-collections-at-runtime">Storing variable type and using it to create collections at runtime</a></p>
  
  <pre><code>static IList createGenericList(Type typeInList) {
      var genericListType = typeof(List&lt;&gt;).MakeGenericType(new[] { typeInList });
      return (IList)Activator.CreateInstance(genericListType);
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>static List&lt;T&gt; createGenericList&lt;T&gt;() {
      List&lt;T&gt; genericListType = new List&lt;T&gt;();
      return genericListType;
  }
  </code></pre>
  
  <p>So here's the different methods and their results:</p>
  
  <pre><code>Type ThisType = typeof(LinkAddy);
  // The normal list method, means I have to copy/paste LinkAddy everywhere
  var fromQuery = new List&lt;LinkAddy&gt;();
  // Makes a List, but I still have to put LinkAddy
  var fromQuery_IsNew = createGenericList&lt;LinkAddy&gt;();
  // Provides a List, I don't have to place LinkAddy
  // but its a List&lt;object&gt; and not List&lt;LinkAddy&gt; so wherever I use
  // the objects, I'll have to cast the object like
  // string title = (fromQuery_IsDiff[0] as LinkAddy).Title;
  var fromQuery_IsDiff = createGenericList(ThisType);
  </code></pre>
  
  <p><strong>Edit:</strong> Eventually, I won't know the type at runtime, the function would be made generic so I could pass LinkAddy as a type or some other type and it would generate the lists as needed. The first step is just to have generics all throughout the function and do is checks on the type for anything that needs to be more specific. This is part of a large function that covers about 200 lines of code that has to do with list validation checks for a database sync (it determines which of the cached and server versions are newer and whichever is newer, post an overwrite to the other if you have permissions to do so). I have multiple classes similar to LinkAddy that are sync using WCF RIA services and SilverLight to a SQL database on the server. The entries are also stored in a class that does XML serialization and saves the XML file to the user's system (cached). This allows my agents in the field to have access to the majority of my site data without an internet connection, as is often the case for them in front of customers.</p>