我正在开发一个mvc3网络应用程序.当用户更新某些东西时,我想将旧数据与用户输入的新数据进行比较,并且将每个不同的字段添加到日志中以创建活动日志.
现在这是我的保存动作的样子:
[HttpPost]
public RedirectToRouteResult SaveSingleEdit(CompLang newcomplang)
{
var oldCompLang = _db.CompLangs.First(x => x.Id == newcomplang.Id);
_db.CompLangs.Attach(oldCompLang);
newcomplang.LastUpdate = DateTime.Today;
_db.CompLangs.ApplyCurrentValues(newcomplang);
_db.SaveChanges();
var comp = _db.CompLangs.First(x => x.Id == newcomplang.Id);
return RedirectToAction("ViewSingleEdit",comp);
}
我发现我可以用这个来迭代我的oldCompLang的属性:
var oldpropertyInfos = oldCompLang.GetType().GetProperties();
但这并不真正有帮助,因为它只显示了属性(Id,Name,Status …),而不是这些属性的值(1,Hello,Ready …).
我可以走艰辛的路:
if (oldCompLang.Status != newcomplang.Status)
{
// Add to my activity log table something for this scenario
}
但是我真的不想为对象的所有属性做这个.
我不知道什么是迭代两个对象以找到不匹配的最佳方法(例如,用户更改了名称或状态…),并从我可以存储在另一个表中的差异构建一个列表.
解决方法
这不是很糟糕,您可以使用反射比较属性“手”,并编写一个扩展方法进行重用 – 您可以将其作为起点:
public static class MyExtensions
{
public static IEnumerable<string> EnumeratePropertyDifferences<T>(this T obj1,T obj2)
{
PropertyInfo[] properties = typeof(T).GetProperties();
List<string> changes = new List<string>();
foreach (PropertyInfo pi in properties)
{
object value1 = typeof(T).GetProperty(pi.Name).GetValue(obj1,null);
object value2 = typeof(T).GetProperty(pi.Name).GetValue(obj2,null);
if (value1 != value2 && (value1 == null || !value1.Equals(value2)))
{
changes.Add(string.Format("Property {0} changed from {1} to {2}",pi.Name,value1,value2));
}
}
return changes;
}
}
