vendredi 2 janvier 2015

Set property value of nullable datetime

Try to update a specific property of my model with Reflection. This works for all other types of my model except properties of type DateTime?


Code:



public void UpdateProperty(Guid topicGuid, string property, string value)
{
var topic = Read(topicGuid);
PropertyInfo propertyInfo = topic.GetType().GetProperty(property);
propertyInfo.SetValue(topic, Convert.ChangeType(value, propertyInfo.PropertyType), null);

topic.DateModified = DateTime.Now;

Save();
}


The following error is thrown on the Convert.ChangeType part:



Invalid cast from 'System.String' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'


How can this be solved?






Aucun commentaire:

Enregistrer un commentaire