5
public class ConflictSaver
{
    private readonly IEnumerable<CommonJobDataInfo> _conflictingJobData;
    private readonly DataAccessDataContext _dc;

    public ConflictSaver(IEnumerable<CommonJobDataInfo> conflictingJobData, DataAccessDataContext dc)
    {
        _conflictingJobData = conflictingJobData;
        _dc = dc;
    }

    public void Save()
    {
        foreach (var data in _conflictingJobData)
        {
            var type = data.ClientBaseObject.GetType();
            var formattedProperty = data.Property.Trim('.').ToUpper();
            foreach (var property in type.GetProperties())
            {
                var currentProperty = data.ClientBaseObject.GetType().GetProperties().First(t => t.Name.Trim() == property.Name.Trim());

                if(currentProperty.Name.ToUpper()== formattedProperty)
                {
                    if (data.UseServerValue)
                        currentProperty.SetValue(currentProperty, data.ServerValue, null);
                    else
                        currentProperty.SetValue(currentProperty, data.ClientValue, null);
                }
            }

        }
    }
}  

当我尝试调用 SetValue() 时,我得到Object 与 Save() 函数中的目标类型不匹配。我反省时发臭。

4

1 回答 1

3

看起来您使用了错误的重载,并传递了错误的对象:

if (data.UseServerValue)
    currentProperty.SetValue(data.ClientBaseObject, data.ServerValue);
else
    currentProperty.SetValue(data.ClientBaseObject, data.ClientValue);

该属性属于data.ClientBaseObject,所以它应该是调用目标。该属性没有索引器,因此如果您使用的是 .NET 4.5 或更高版本,则可以完全跳过第三个参数。

于 2013-03-05T19:50:36.800 回答