我正在编写一个 Windows 商店应用程序,它从服务器接收对象(我创建的自定义类型),让用户编辑对象,然后将“变更集”提交回服务器。这个变更集只是一个与接收到的对象类型相同的对象,除了用户编辑的字段之外,每个字段都设置为 null。然后这些字段包含用户的编辑,如下所示:
原案: 描述:“排气管破裂” 状态:“开放” 顾客: ”” 里程:10000 编辑案例: 描述: ”” 状态“开放” 客户:“示例公司” 里程:10000 案例变更集: 描述: ”” 状态:空 客户:“示例公司” 里程:无
当应用程序第一次从服务器下载对象时,我会复制它以供以后比较。然后用户对其中一个对象进行更改,当用户提交他的更改时,使用通用方法计算这两个对象的变更集CalculateChangeSet
。它遍历两个对象的每个属性并比较它们是否相等:
public static T CalculateChangeSet<T>(T oldObject, T newObject) where T : new()
{
T changeSet = (T)Activator.CreateInstance(oldObject.GetType());
foreach (PropertyInfo property in oldObject.GetType().GetRuntimeProperties())
{
var oldValue = property.GetValue(oldObject);
var newValue = newObject.GetType().GetRuntimeProperty(property.Name).GetValue(newObject);
if (oldValue != null && newValue != null)
{
if (oldValue is IList)
{
Type listType = oldValue.GetType().GetRuntimeProperty("Item").PropertyType;
IList<listType> oldList = (IList<listType>)oldValue; //Visual studio complains about
IList<listType> newList = (IList<listType>)newValue; //listType not being found
if (!oldList.SequenceEqual(newList))
{
changeSet.GetType().GetRuntimeProperty(property.Name).SetValue(changeSet, newValue, null);
}
}
else
{
if (!oldValue.Equals(newValue))
{
changeSet.GetType().GetRuntimeProperty(property.Name).SetValue(changeSet, CalculateChangeSet(oldValue, newValue));
}
}
}
else
{
changeSet.GetType().GetRuntimeProperty(property.Name).SetValue(changeSet, newValue);
}
}
return changeSet;
}
该方法适用于我遇到的每个属性,除了 Lists,所以我创建了 if 子句来处理列表。由于list1.Equals(list2)
不比较列表中的项目,我们需要强制转换oldValue
和newValue
toList<T>
才能使用list.SequenceEqual()
.
为什么我The type or namespace name 'listType' could not be found (are you missing a using directive or an assembly reference?)
在尝试使用它来创建新列表时收到错误消息?如果有更好的方法来解决这个问题,我愿意接受建议..