C# Reflection Object does not match target type

I'm trying to use the propertyInfo.SetValue() method to set an object property value with reflection, and I'm getting the exception "Object does not match target type". It doesn't really make sense (at least to me!) as I'm just trying to set a simple string property on an object with a string replacement value. Here's a code snippet - this is contained within a recursive function so there's a bunch more code, but this is the guts:

PropertyInfo fieldPropertyInfo = businessObject.GetType().GetProperties().FirstOrDefault(f => f.Name.ToLower() == piecesLeft[0].ToLower());
businessObject = fieldPropertyInfo.GetValue(businessObject, null);

fieldPropertyInfo.SetValue(businessObject, replacementValue, null);

I've verified that "businessObject" and "replacementValue" are both the same type by doing this comparison, which returned true:

businessObject.GetType() == replacementValue.GetType()
Jon Skeet
people
quotationmark

I suspect you just want to remove the second line. What's it doing there anyway? You're fetching the value of the property from the object referred to by businessObject - and setting that to the new value of businessObject. So if this really is a string property, the value of businessObject will be a string reference afterwards - and you're then trying to use that as the target for setting the property! It's a bit like doing this:

dynamic businessObject = ...;
businessObject = businessObject.SomeProperty; // This returns a string, remember!
businessObject.SomeProperty = replacementValue;

That's not going to work.

It's not clear what replacementValue is - whether it's the replacement string or a business object to fetch the real replacement value from, but I suspect you either want:

PropertyInfo fieldPropertyInfo = businessObject.GetType().GetProperties()
      .FirstOrDefault(f => f.Name.ToLower() == piecesLeft[0].ToLower());
fieldPropertyInfo.SetValue(businessObject, replacementValue, null);

Or:

PropertyInfo fieldPropertyInfo = businessObject.GetType().GetProperties()
      .FirstOrDefault(f => f.Name.ToLower() == piecesLeft[0].ToLower());
object newValue = fieldPropertyInfo.GetValue(replacementValue, null);
fieldPropertyInfo.SetValue(businessObject, newValue, null);

people

See more on this question at Stackoverflow