Convert string to type in object
See the question and my original answer on StackOverflowThere is no absolute answer that works for all types. But, you could use a TypeConverter instead of Convert, it usually works better. For example, there is a TimeSpanConverter:
public void Update<T>(string fieldName, string fieldValue)
{
System.Reflection.PropertyInfo propertyInfo = typeof(T).GetProperty(fieldName);
Type propertyType = propertyInfo.PropertyType;
TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter.CanConvertFrom(typeof(string)))
{
var a = converter.ConvertFrom(fieldValue, type);
...
}
}