using Microsoft.Ajax.Utilities; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Web; using static Telerik.Web.UI.OrgChartStyles; namespace VRS.Util { public static class CompareObject { public static bool IsConsistent(T t, T1 t1, T2 t2, out string field, out string field_yes, out string field_no, string dbwx = "2") { field = ""; field_yes = ""; field_no = ""; string st = ""; bool Istrue = true; System.Reflection.FieldInfo[] fields = t.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); System.Reflection.FieldInfo[] fields2 = t1.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (fields.Length > 0 && fields.Length > 0 && fields.Length == fields2.Length) { foreach (System.Reflection.FieldInfo item in fields) { string name = item.Name; //名称 if (name.Contains("cmdlist_ms")) { continue; } if (dbwx == "1" && name.Contains("ippz_wangkou")) { continue; } object value = item.GetValue(t); //值 if (value == null) { value = ""; } var field_list = fields2.Where(x => x.Name == item.Name); if (field_list.Count() > 0) { var field_model = field_list.FirstOrDefault(); if (field_model != null) { object value_compare = field_model.GetValue(t1); //值 if (value != null && value.GetType().IsGenericType) { if (name.Contains("clgl_xy") || name.Contains("mqtt_sjgl")) { List obj_compare = new List(); List obj = (List)value; if (value_compare != null) { obj_compare = (List)value_compare; } if (obj.Count() == obj_compare.Count()) { var camBom = obj.OrderBy(x => x).ToList(); var vBom = obj_compare.OrderBy(x => x).ToList(); //因为SequenceEqual 比对 顺序不一致 返回false,因此先排序 if (!Enumerable.SequenceEqual(camBom, vBom)) { field += "【" + item.ReflectedType.Name + "】"; if (value != null) { field_yes += "【" + JsonConvert.SerializeObject(value) + "】"; } else { field_yes += "【】"; } if (value_compare != null) { field_no += "【" + JsonConvert.SerializeObject(value_compare) + "】"; } else { field_no += "【】"; } Istrue = false; } } else { field += "【" + item.ReflectedType.Name + "】"; if (value != null) { field_yes += "【" + JsonConvert.SerializeObject(value) + "】"; } else { field_yes += "【】"; } if (value_compare != null) { field_no += "【" + JsonConvert.SerializeObject(value_compare) + "】"; } else { field_no += "【】"; } Istrue = false; } } else { IEnumerable obj = (IEnumerable)value; IEnumerable obj_compare = (IEnumerable)value_compare; if (obj.Count() == obj_compare.Count()) { Dictionary> dic_list = new Dictionary>(); Dictionary> dic_or_list = new Dictionary>(); foreach (var oj2 in obj_compare) { System.Reflection.FieldInfo[] oj2_fields = oj2.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); string field_name = ""; Dictionary dic = new Dictionary(); foreach (System.Reflection.FieldInfo of in oj2_fields) { var reflected_name = of.Name.Replace("<", "").Replace(">", "").Replace("k__BackingField", ""); if (reflected_name == "dx_name") { field_name = of.GetValue(oj2).ToString(); } object value_oj = of.GetValue(oj2); if (dic != null && dic.Where(x => x.Key.Contains(reflected_name)).Count() > 0) { reflected_name += dic.Where(x => x.Key.Contains(reflected_name)).Count() + 1; } dic.Add(reflected_name, value_oj.ToString()); } if (dic_list != null && dic_list.Where(x => x.Key.Contains(field_name)).Count() > 0) { field_name += dic_list.Where(x => x.Key.Contains(field_name)).Count() + 1; } dic_list.Add(field_name, dic); } foreach (var oj2 in obj) { System.Reflection.FieldInfo[] oj2_fields = oj2.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); string field_name = ""; Dictionary dic = new Dictionary(); foreach (System.Reflection.FieldInfo of in oj2_fields) { var reflected_name = of.Name.Replace("<", "").Replace(">", "").Replace("k__BackingField", ""); if (reflected_name == "dx_name") { field_name = of.GetValue(oj2).ToString(); } object value_oj = of.GetValue(oj2); if (dic != null && dic.Where(x => x.Key.Contains(reflected_name)).Count() > 0) { reflected_name += dic.Where(x => x.Key.Contains(reflected_name)).Count() + 1; } dic.Add(reflected_name, value_oj.ToString()); } if (dic_or_list.Where(x => x.Key.Contains(field_name)).Count() > 0) { field_name += dic_or_list.Where(x => x.Key.Contains(field_name)).Count() + 1; } dic_or_list.Add(field_name, dic); } // new added string temp1 = "";// string last_yes1 = ""; // string temp2 = ""; string last_no2 = ""; // foreach (var dic in dic_or_list) { if (dic_list.Where(x => x.Key.Contains(dic.Key)).Count() > 0) { var dc_list_2 = dic_list.Where(x => x.Key.Contains(dic.Key)); foreach (var dc in dc_list_2) { Dictionary dic_or = dic.Value; foreach (var or in dic_or) { var m = dc.Value.Where(x => x.Key == or.Key).FirstOrDefault(); if (!Equals(or.Value, m.Value)) { field += "【" + item.Name.Replace("<", "").Replace(">", "").Replace("k__BackingField", "") + "】"; if (value != null) { temp1 = "【" + JsonConvert.SerializeObject(value) + "】"; /* if (last_yes1!= temp1) { field_yes += temp1; last_yes1 = temp1; }*/ if (field_yes.IndexOf(temp1) < 0) { field_yes += temp1; } //field_yes += "【" + JsonConvert.SerializeObject(value) + "】"; //$$$$ } else { field_yes += "【】"; } if (value_compare != null) { temp2 = "【" + JsonConvert.SerializeObject(value_compare) + "】"; if (field_no.IndexOf(temp2) < 0) { field_no += temp2; } /* if (last_no2 != temp2) { field_no += temp2; last_no2 = temp2; } */ //field_no += "【" + JsonConvert.SerializeObject(value_compare) + "】"; //$$$$ } else { field_no += "【】"; } Istrue = false; } } } } else { field += "【" + item.Name.Replace("<", "").Replace(">", "").Replace("k__BackingField", "") + "】"; if (value != null) { field_yes += "【" + JsonConvert.SerializeObject(value) + "】"; } else { field_yes += "【】"; } if (value_compare != null) { field_no += "【" + JsonConvert.SerializeObject(value_compare) + "】"; } else { field_no += "【】"; } Istrue = false; } } } else { field += "【" + item.Name.Replace("<", "").Replace(">", "").Replace("k__BackingField", "") + "】"; if (value != null) { field_yes += "【" + JsonConvert.SerializeObject(value) + "】"; } else { field_yes += "【】"; } if (value_compare != null) { field_no += "【" + JsonConvert.SerializeObject(value_compare) + "】"; } else { field_no += "【】"; } Istrue = false; } } } else { if (!Equals(value, value_compare)) { field += "【" + item.Name.Replace("<", "").Replace(">", "").Replace("k__BackingField", "") + "】"; if (value != null) { field_yes += "【" + value.ToString() + "】"; } else { field_yes += "【】"; } if (value_compare != null) { field_no += "【" + value_compare.ToString() + "】"; } else { field_no += "【】"; } Istrue = false; } } } else { Istrue = false; } } } } else { Istrue = false; } #region 修改判分 cgp $$$$ if (Istrue == false) { /* if (field_yes== "【0】" && field_no=="【false】") { field_yes = ""; field_no = ""; Istrue = true; } else if (field_yes == "【1】" && field_no == "【true】") { field_yes = ""; field_no = ""; Istrue = true; } */ /* if (field_yes == "【0】【1】【0】" && field_no == "【false】【true】【false】") { field = ""; field_yes = ""; field_no = ""; Istrue = true; return Istrue; } */ field_no = field_no.Replace("【false】", "【0】").Replace("【true】", "【1】"); if (field_yes == field_no) { field = ""; field_yes = ""; field_no = ""; Istrue = true; return Istrue; } } #endregion return Istrue; } public static bool IsConsistentList(List t_list, List t1_list, T2 t2, out string field, out string field_yes, out string field_no) { field = ""; field_yes = ""; field_no = ""; string st = ""; if (t_list.Count() > 0 && t1_list.Count() > 0 && t_list.Count() == t1_list.Count()) { Dictionary> dic_list = new Dictionary>(); Dictionary> dic_or_list = new Dictionary>(); foreach (T t in t_list) { System.Reflection.FieldInfo[] fields = t.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); string field_name = ""; Dictionary dic = new Dictionary(); foreach (System.Reflection.FieldInfo of in fields) { var reflected_name = of.Name.Replace("<", "").Replace(">", "").Replace("k__BackingField", ""); //if (reflected_name == "wgid" || reflected_name == "csname") //old if (reflected_name == "wgbh" || reflected_name == "csname") { field_name = of.GetValue(t).ToString(); } object value_oj = of.GetValue(t); if (value_oj != null) { dic.Add(reflected_name, value_oj.ToString()); } else { dic.Add(reflected_name, ""); } } dic_or_list.Add(field_name, dic); } foreach (T1 t in t1_list) { System.Reflection.FieldInfo[] fields = t.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); string field_name = ""; Dictionary dic = new Dictionary(); foreach (System.Reflection.FieldInfo of in fields) { var reflected_name = of.Name.Replace("<", "").Replace(">", "").Replace("k__BackingField", ""); if (reflected_name == "wgbh" || reflected_name == "csname") { field_name = of.GetValue(t).ToString(); } object value_oj = of.GetValue(t); if (dic.Where(x => x.Key.Contains(reflected_name)).Count() > 0) { reflected_name += "|" + dic.Where(x => x.Key.Contains(reflected_name)).Count() + 1; } if (value_oj != null) { dic.Add(reflected_name, value_oj.ToString()); } else { dic.Add(reflected_name, ""); } } if (dic_list != null && dic_list.Where(x => x.Key.Contains(field_name)).Count() > 0) { field_name += "|" + dic_list.Where(x => x.Key.Contains(field_name)).Count() + 1; } dic_list.Add(field_name, dic); } foreach (var dic in dic_or_list) { if (dic_list.Where(x => x.Key.Contains(dic.Key)).Count() > 0) { var dc_list_2 = dic_list.Where(x => x.Key.Contains(dic.Key)); foreach (var dc in dc_list_2) { Dictionary dic_or = dic.Value; foreach (var or in dic_or) { var m_list = dc.Value.Where(x => x.Key.Contains(or.Key)); foreach (var m in m_list) { if (!Equals(or.Value, m.Value)) { if (t_list != null) { field_yes = "【" + JsonConvert.SerializeObject(t_list) + "】"; } else { field_yes = "【】"; } if (t1_list != null) { field_no = "【" + JsonConvert.SerializeObject(t1_list) + "】"; } else { field_no = "【】"; } return false; } } } } } else { if (t_list != null) { field_yes = "【" + JsonConvert.SerializeObject(t_list) + "】"; } else { field_yes = "【】"; } if (t1_list != null) { field_no = "【" + JsonConvert.SerializeObject(t1_list) + "】"; } else { field_no = "【】"; } return false; } } } else { if (t_list != null) { field_yes = "【" + JsonConvert.SerializeObject(t_list) + "】"; } else { field_yes = "【】"; } if (t1_list != null) { field_no = "【" + JsonConvert.SerializeObject(t1_list) + "】"; } else { field_no = "【】"; } return false; } return true; } } }