C# Reflection - Object does not match target type

You're trying to set the value of the propertyinfo value's. Because you're overwriting the businessObject

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

// The result should be stored into another variable here:
businessObject = fieldPropertyInfo.GetValue(businessObject, null);

fieldPropertyInfo.SetValue(businessObject, replacementValue, null);

It should be something like:

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

// also you should check if the propertyInfo is assigned, because the 
// given property looks like a variable.
if(fieldPropertyInfo == null)
    throw new Exception(string.Format("Property {0} not found", f.Name.ToLower()));

// you are overwriting the original businessObject
var businessObjectPropValue = fieldPropertyInfo.GetValue(businessObject, null);

fieldPropertyInfo.SetValue(businessObject, replacementValue, null);

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);

You're trying to set the value of the property on businessObject to another value of the type of businessObject, not the type of that property.

For this code to work, replacementValue needs to be the same type as the field defined by piecesLeft[0], and it obviously is not that type.

Tags:

C#

Reflection