开发者

Invoking setter with return value from invoked getter, exception:

开发者 https://www.devze.com 2023-01-31 22:19 出处:网络
I\'m attempting to do something like this: object value = sourceProperty.Invoke(this, new object[] { });

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号