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()
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 ofbusinessObject
. So if this really is a string property, the value ofbusinessObject
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: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:Or:
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 bypiecesLeft[0]
, and it obviously is not that type.You're trying to set the value of the propertyinfo value's. Because you're overwriting the
businessObject
It should be something like: