title: How do I get the generic, T, of a List<T> field and use it as a generic of another constructor?
body: <p>I am writing a <code>JsonMapper</code> in order to map a JSON String to the fields of a C# class using <a href="https://wiki.unity3d.com/index.php/SimpleJSON" rel="nofollow noreferrer">SimpleJSON</a>, similar to <code>JSONUtility</code> so that I can have greater control over the mapping. I'm not inclined to write the code for mapping each class as I will have many multiple classes with varying fields.</p>
  
  <pre><code>public class JsonMapper&lt;T&gt;
  {
      public string jsonString;
      public T mappedObj;
  
      public JsonMapper(string json)
      {
          jsonString = json;
          JSONNode jn = JSON.Parse(json);
  
          Type t = Assembly.GetExecutingAssembly().GetType(typeof(T).FullName);
          mappedObj = (T) Activator.CreateInstance(t);
  
          foreach (FieldInfo field in typeof(T).GetFields())
          {
              switch (field.FieldType.Name)
              {
                  case "Int32":
                      field.SetValue(mappedObj, int.Parse(jn[field.Name]));
                      Debug.Log(field + ": " + field.GetValue(mappedObj));
                      break;
                      ...
  </code></pre>
  
  <p>I've used reflection in order to map primitive data types so far, but currently I am stuck trying to map lists. I'm trying to obtain the generic, <code>T</code>, of the <code>List</code>, and recursively create another instance of my <code>JsonMapper</code>. Below is an example of what I am trying to achieve:</p>
  
  <pre><code>case "List`1":
      Type genericType = field.FieldType.GetGenericArguments()[0];
      foreach (var genericJson in jn[field.Name])
      {
          //This line does not work, it is pseudocode
          JsonMapper&lt;genericType&gt; recurJM = new JsonMapper&lt;genericType&gt;(genericJson.ToString());
      }
      break;
  </code></pre>
  
  <p>To do so I have referenced <a href="https://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method">this</a> to invoke generic methods using reflection and <a href="https://stackoverflow.com/questions/3255697/using-c-sharp-reflection-to-call-a-constructor">this</a> to invoke constructors. However, I can't seem to find a way to invoke a constructor with generics. </p>
  
  <p>My question is: How do I achieve this particular line of code in the above code snippet?</p>
  
  <pre><code>JsonMapper&lt;genericType&gt; recurJM = new JsonMapper&lt;genericType&gt;(genericJson.ToString());
  </code></pre>