I would like to implement a general purpose runtime type conversion function that makes use .Net TypeConverters to do the conversion.
Does anyone know how to how t
TypeConverter converter = TypeDescriptor.GetConverter(sourceType);
if(converter.CanConvertTo(destinationType)) {
return converter.ConvertTo(obj, destinationType);
}
converter = TypeDescriptor.GetConverter(destinationType);
if(converter.CanConvertFrom(sourceType)) {
return converter.ConvertFrom(obj);
}
You could also look at Convert.ChangeType(obj, destinationType)