I already found out that it is possible to set the value of a property using reflection: Set object property using reflection
But my problem is that my data exists only as string. Therefore of course I always get an exception because it is not the right type.
Is there a way of automatically trying to parse the string to the according type (DateTime, int, decimal, float)?
Below is the code I'm using:
Type myType = obj.GetType();
PropertyInfo[] props = myType.GetProperties();
foreach (PropertyInfo prop in props)
{
setProperty(obj, prop, data[prop.Name]);
}
data
is a simple associative array that contains the data as string. These data are supposed to be mapped into obj
.