My scenario should be simple... the type I want to convert FROM is ALWAYS 'string'. What I want to convert to... could be many things - ints, DateTimes, ... strings, etc.
This would be easy:
string valueToConvertFrom = "123";
int blah = Convert.ToInt32(valueToConvertFrom);
However... I don't know (until runtime) that the value I need to convert to is an 'Int' (or whatever). I have tried this:
string valueToConvertFrom = "123";
Type convertToType = typeof(int);
object blah = Convert.ChangeType(valueToConvertFrom, convertToType);
But that gives me the following error: "Object must implement IConvertible."
I don't want to have to do a switch statement and call "Convert.ToBlah" based on the type name... any suggestions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…