SetValue on PropertyInfo instance error “Object does not match target type” c#

前端 未结 2 1859
礼貌的吻别
礼貌的吻别 2020-12-17 11:15

Been using a Copy method with this code in it in various places in previous projects (to deal with objects that have same named properties but do not derive from a common ba

相关标签:
2条回答
  • 2020-12-17 11:20

    Try:

    toProperty.SetValue(toObj,fromValue,null);
    

    You are trying to pass in the property (toProperty) as the target object, instead of toObj. For info, if you are doing lots of this, maybe consider HyperDescriptor, which can vastly reduce the reflection cost.

    0 讨论(0)
  • 2020-12-17 11:27

    Should be

    toProperty.SetValue(toObj,fromValue,null);
    
    0 讨论(0)
提交回复
热议问题