I found this great post by @hugoware about parsing values: http://hugoware.net/blog/more-control-when-parsing-values. I re-used his code sample in a project but now I noticed in the last block (line 154 of his code) he uses the Convert.ChangeType() method as a last attempt to "convert" the value.
Now I wonder if this makes sense, since we're always starting from a string value and I guess Convert.ChangeType only does casting on value types? Does it make sense to try that or will it always fail?
If you just want to convert strings, I advice you to use ConvertToString / ConvertFromString
TypeConverter converter = TypeDescriptor.GetConverter(type);
string res = converter.ConvertToString(obj);
object original = converter.ConvertFromString(res);
--
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With