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

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

posted @   W(王甜甜)  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示