Invoking setter with return value from invoked getter, exception:
I'm attempting to do something like this:
object value = sourceProperty.Invoke(this, new object[] { });
targetProperty.Invoke(target, new object[] { value });
where source/targetProperty values are MethodInfo objects. At the time that this code is executed, there is already a guarantee that the properties are of the same type, and that both the this object and the target object have those properties on them.
This works most of the time, but I'm running into an exception when the property type is int:
"TargetException: Object does not match target type."
Any idea what could be going on here?
ED开发者_运维技巧IT: This is resolved, see comments.
It turns out I was using the PropertyInfo[] from the this object instead of the target when I was trying to find the property that matches the name of the source property.
The target object did actually have the property it was supposed to have, and the property types matched, and the types of the object returned from the source property matched the target property (which was erroneously the setter of the this object instead of the setter of the target object), so nothing pointed to this.
I noticed the issue while formatting the block of code for an edit to my question.
精彩评论