通过反射对比两个Model值的差异

发布时间 2023-10-24 14:30:00作者: 墨秀铜香

//Head
var head = row.Head.GetType().GetProperties();
var headModel = model.Head.GetType().GetProperties();
foreach (var h in head)
{
if (h.Name != "ChangedBy" & h.Name != "UpdateDate" & h.Name != "UpDate" & h.Name != "UpdateUser")
{
foreach (var hc in headModel)
{
if (h.Name == hc.Name)
{
if (h.PropertyType == typeof(bool))
{
bool v = (bool)h.GetValue(row.Head);
bool vc = (bool)hc.GetValue(model.Head);
if (v != vc)
{
rows.Add(string.Format("Head {0}:{1}-{2}", h.Name, v, vc));
}
}
else if (h.PropertyType == typeof(DateTime))
{
var v = (DateTime)h.GetValue(row.Head);
var vc = (DateTime)hc.GetValue(model.Head);
if (v != vc)
{
rows.Add(string.Format("Head {0}:{1}-{2}", h.Name, v, vc));
}
}
else if (h.PropertyType == typeof(decimal))
{
decimal v = (decimal)h.GetValue(row.Head);
decimal vc = (decimal)hc.GetValue(model.Head);
if (v != vc)
{
rows.Add(string.Format("Head {0}:{1}-{2}", h.Name, v, vc));
}
}
else if (h.PropertyType == typeof(int))
{
int v = (int)h.GetValue(row.Head);
int vc = (int)hc.GetValue(model.Head);
if (v != vc)
{
rows.Add(string.Format("Head {0}:{1}-{2}", h.Name, v, vc));
}
}
else
{
string v = h.GetValue(row.Head) != null ? h.GetValue(row.Head).ToString() : "";
string vc = hc.GetValue(model.Head) != null ? hc.GetValue(model.Head).ToString() : "";
if (v != vc)
{
rows.Add(string.Format("Head {0}:{1}-{2}", h.Name, v, vc));
}
}
}
}
}
}