title: Dynamically create an array of Type in C#
body: <p>In C#, I need to be able to create an array of Type objects at run-time based on a comma-delimited list of data types passed in to a function as a string. Basically, here is what I am trying to accomplish:</p>
  
  <pre><code>// create array of types
  Type[] paramTypes = { typeof(uint), typeof(string), typeof(string), typeof(uint) };
  </code></pre>
  
  <p>But I need to be able to call my function like this:</p>
  
  <pre><code>MyFunction("uint, string, string, uint");
  </code></pre>
  
  <p>and have it generate the array dynamically based on the string passed in. Here was my first attempt:</p>
  
  <pre><code>void MyFunction(string dataTypes)
  {
      //out or in parameters of your function.   
      char[] charSeparators = new char[] {',', ' '};
      string[] types = dataTypes.Split(charSeparators,
                          stringSplitOptions.RemoveEmptyEntries);
  
      // create a list of data types for each argument
      List&lt;Type&gt; listTypes = new List&lt;Type&gt;();
      foreach (string t in types)
      {
          listTypes.Add(Type.GetType(t)); 
      }
      // convert the list to an array
      Type [] paramTypes = listTypes.ToArray&lt;Type&gt;();
  
  }
  </code></pre>
  
  <p>This code simply creates an array of null objects of type System.Type. I'm pretty sure the problem lies here:</p>
  
  <pre><code>listTypes.Add(Type.GetType(t));
  </code></pre>
  
  <p>Suggestions on why this syntax does not do the trick?</p>