对比两个Model的不同之处并返回JSON


/// <summary>
/// 对比两个Model的不同之处
/// </summary>
/// M M1 = new M();
/// M M2 = new M();
/// M1.id = 1;
/// M1.guid = "123";
/// M2.id = 55555;
/// M2.guid = "13323";
/// PropertyInfo[] mPi = typeof(M).GetProperties();
/// string ret = GetContrastModel(M1, M2, mPi);
/// <param name="M1">old</param>
/// <param name="M2">new</param>
/// <param name="mPi"></param>
/// <returns></returns>
public static string GetContrastModel(object M1, object M2, string type)
{
string strret = "\"" + type + "\":[{0}]";
List<string> list = new List<string>();
List<string> listnew = new List<string>();
PropertyInfo[] mPi = M1.GetType().GetProperties();
for (int i = 0; i < mPi.Length; i++)
{
PropertyInfo pi = mPi[i];
var oldValue = pi.GetValue(M1, null) == null ? "空" : pi.GetValue(M1, null).ToString();
var newValue = pi.GetValue(M2, null) == null ? "空" : pi.GetValue(M2, null).ToString();
if (oldValue != newValue)
{
if (M1.GetType().Name == "tb_房屋对象")
{
if (pi.Name == "实物描述" || pi.Name == "实物分析" || pi.Name == "权益描述" || pi.Name == "区位描述")
{
continue;
}
}
if (M1.GetType().Name == "tb_土地对象")
{
if (pi.Name == "实物描述" || pi.Name == "实物分析" || pi.Name == "权益描述" || pi.Name == "区位描述")
{
continue;
}
}

// @"&S.*?&E"
if (Common.JsonSplit.IsJsonStart(oldValue))
{
list.Add("\"" + pi.Name + "\":&S" + oldValue + "&E");
}
else
{
list.Add("\"" + pi.Name + "\":\"" + oldValue + "\"");
}
if (Common.JsonSplit.IsJsonStart(newValue))
{
listnew.Add("\"" + pi.Name + "\":&S" + newValue + "&E");
}
else
{
listnew.Add("\"" + pi.Name + "\":\"" + newValue + "\"");
}
}
}
if (list == null || list.Count == 0)
{
return "没有修改任何值。";
}
else
{
return "{" + string.Format(strret, "{" + Toolbox.ListToStringSql(list) + "},{" + Toolbox.ListToStringSql(listnew) + "}") + "}";
}
}

posted @ 2016-05-06 14:23  DinnisZhang  阅读(734)  评论(0)    收藏  举报