title: Convert string to type in object
body: <p>The code I have to implement takes a posted list of data from an Ajax call from a web page.</p>
  
  <p>I know the object I need to update, but each field/value pair is coming through as string values and not as their proper types.</p>
  
  <p>So I am trying to work out the type of the property, casting the value as the new type and then apply that to the field using reflection.</p>
  
  <p>However I am getting the following error for anything other than strings.</p>
  
  <pre><code>Invalid cast from 'System.String' to 'System.TimeSpan'.
  </code></pre>
  
  <p>The code I am attempting the conversion in is;</p>
  
  <pre><code>    public void Update&lt;T&gt;(string fieldName, string fieldValue)
      {
          System.Reflection.PropertyInfo propertyInfo = typeof(T).GetProperty(fieldName);
          Type propertyType = propertyInfo.PropertyType;
  
          var a = Convert.ChangeType(fieldValue, propertyType);
      }
  </code></pre>
  
  <p>So  is the target object.</p>