I've a similar error to this one, but unfortunately not the same simple solution. Here is the code:
public virtual void MapObject(T obj, IViewModel<T> viewModel, ITPSDataAccess dataAccess)
{
var objProps = obj.GetType().GetProperties();
var dtoProps = viewModel.GetType().GetProperties();
foreach (var dtoProp in dtoProps)
{
var objProp = dtoProps.SingleOrDefault(x => x.Name == dtoProp.Name);
if (objProp != null)
{
var dtoVal = dtoProp.GetValue(viewModel, null);
objProp.SetValue(obj, dtoVal, null); // ERROR HERE
}
}
...
}
The error occurs at the point indicated, stating "Object does not match target type". It looks like I'm passing the correct object to be set - I am therefore unable to solve the problem.
I've also tried to take the advice of this solution, and examine the types of property I'm attempting to set which throw the exception - the property in question is a string, and the setter does not appear to be broken since it works under normal, non-reflection circumstances.