【问题标题】:C# Reflection - Object does not match target typeC# 反射 - 对象与目标类型不匹配
【发布时间】:2013-10-06 17:30:40
【问题描述】:

我正在尝试使用 propertyInfo.SetValue() 方法通过反射设置对象属性值,但出现异常“对象与目标类型不匹配”。这真的没有意义(至少对我来说!),因为我只是想在一个带有字符串替换值的对象上设置一个简单的字符串属性。这是一个代码 sn-p - 它包含在一个递归函数中,所以还有很多代码,但这是胆量:

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

fieldPropertyInfo.SetValue(businessObject, replacementValue, null);

通过比较,我已经验证 businessObject" andreplacementValue` 是同一类型,返回 true:

businessObject.GetType() == replacementValue.GetType()

【问题讨论】:

    标签: c# reflection


    【解决方案1】:

    您正在尝试设置 propertyinfo 值的值。因为你覆盖了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);
    

    应该是这样的:

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

    【讨论】:

    • bingo - 感谢您提供清晰简洁的代码示例。谢谢!
    【解决方案2】:

    我怀疑你只是想删除第二行。它到底在那儿做什么?您正在 businessObject 引用的对象中获取属性的值 - 并将其设置为businessObject 的新值。因此,如果这确实是一个字符串属性,那么 businessObject 的值之后将是一个字符串引用 - 然后您尝试将其用作 setting 属性的目标!有点像这样做:

    dynamic businessObject = ...;
    businessObject = businessObject.SomeProperty; // This returns a string, remember!
    businessObject.SomeProperty = replacementValue;
    

    那是行不通的。

    尚不清楚replacementValue 是什么 - 无论是替换字符串还是从中获取真正替换值的业务对象,但我怀疑你要么想要:

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

    或者:

    PropertyInfo fieldPropertyInfo = businessObject.GetType().GetProperties()
          .FirstOrDefault(f => f.Name.ToLower() == piecesLeft[0].ToLower());
    object newValue = fieldPropertyInfo.GetValue(replacementValue, null);
    fieldPropertyInfo.SetValue(businessObject, newValue, null);
    

    【讨论】:

      【解决方案3】:

      您正在尝试将 businessObject 上的属性的值 设置为 businessObject 类型的另一个值,而不是该属性的类型。

      要使这段代码正常工作,replacementValue 需要与piecesLeft[0] 定义的字段类型相同,而它显然不是那种类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 2018-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多