title: Convert string (System.String) to type
body: <p>I am trying to create a generic XML to object converter. In other words, the following is my XML</p>
  
  <pre><code>&lt;setting&gt;
     &lt;name&gt;testing&lt;/name&gt; 
     &lt;type&gt;System.String&lt;/type&gt;
     &lt;defaultObj&gt;TTTT&lt;/defaultObj&gt;
  &lt;/setting&gt;
  </code></pre>
  
  <p>the type field holds the type of the object its loading back in. This is just the object structure I serailized in. Regardless, I am having an issue converting</p>
  
  <pre><code>System.String
  </code></pre>
  
  <p>into an actual type variable. So for instance, in order to convert I have the following code:</p>
  
  <pre><code>foreach (XNode node in document.Element(root).Nodes())
  {
     T variable = new T(); //where T : new()
  
     foreach (FieldInfo field in fields)
     {
        field.SetValue(variable, Convert.ChangeType(((XElement)node).Element(field.Name).Value, field.FieldType));
     }
  
     retainedList.Add(variable);
  }
  </code></pre>
  
  <p>which obtains objects back in a generic way. The algorithm works perfectly until it comes across the Type field. I get a:</p>
  
  <pre><code>Invalid cast from 'System.String' to 'System.Type'.
  </code></pre>
  
  <p>run time error. From what I can tell it is having an issue directly converting a type identifier (string) directly into a type. I am not sure how to get around this issue, atleast when it comes to keeping things generic and clean. Any ideas? I am sorry if the issue is a bit vague, if you don't quite understand I will try to clarify further. Any help is greatly appreciated!</p>