Quote:
Convert.ChangeType(newVal, propertyType);
propInfo.SetValue(myObjectFromDB, newVal);
There's your problem -
Convert.ChangeType
returns the result of the conversion. It doesn't change the type of the object you've passed in.
You'll also need to use
property.PropertyType
instead of
property.GetType()
. The former returns the declared type of the property; the latter will return
typeof(PropertyInfo)
.
EDIT: Looks like you're also going to have a problem with the conversion -
ChangeType
can't cope with converting a string to a nullable type, and gives you an
InvalidCastException
. You'll need to convert to the underlying type instead.
Try changing your code to:
Type propertyType = property.PropertyType;
Type realPropertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;
object newPropertyValue = Convert.ChangeType(newVal, realPropertyType);
propInfo.SetValue(myObjectFromDB, newPropertyValue);