I have a Type, a String and an Object.
Is there some way I can call the parse method or convert for that type on the string dynamically?
Basically how do I remove the if statements in this logic
object value = new object();
String myString = "something";
Type propType = p.PropertyType;
if(propType == Type.GetType("DateTime"))
{
value = DateTime.Parse(myString);
}
if (propType == Type.GetType("int"))
{
value = int.Parse(myString);
}
And do someting more like this.
object value = new object();
String myString = "something";
Type propType = p.PropertyType;
//this doesn't actually work
value = propType .Parse(myString);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…