public class CompareModelHelper
{
public static string CompareModel<T>(T oldModel, T newModel) where T : class
{
string json = string.Empty;
List<LogItem> logItems = new List<LogItem>();
if (newModel != null && oldModel == null)
{
PropertyInfo[] properties = newModel.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
object newValue = item.GetValue(newModel);
LogItem logItem = new LogItem();
logItem.Property = name;
logItem.Old_Value = null;
logItem.New_Value = newValue!=null?newValue.ToString():null;
logItems.Add(logItem);
}
json = JsonConvert.SerializeObject(logItems);
}
else if (newModel != null && oldModel != null)
{
PropertyInfo[] properties = oldModel.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
object oldValue = item.GetValue(oldModel);
object newValue = item.GetValue(newModel);
if (oldValue != null && newValue!=null)
{
if (name == "ID")
{
LogItem logItem = new LogItem();
logItem.Property = name;
logItem.Old_Value = oldValue.ToString();
logItem.New_Value = null;
logItems.Add(logItem);
}
if (!oldValue.Equals(newValue))
{
LogItem logItem = new LogItem();
logItem.Property = name;
logItem.Old_Value = oldValue.ToString();
logItem.New_Value = newValue.ToString();
logItems.Add(logItem);
}
}
else if (oldValue!=null && newValue==null)
{
LogItem logItem = new LogItem();
logItem.Property = name;
logItem.Old_Value = oldValue.ToString();
logItem.New_Value = null;
logItems.Add(logItem);
}
else if (oldValue == null && newValue != null)
{
LogItem logItem = new LogItem();
logItem.Property = name;
logItem.Old_Value = null;
logItem.New_Value = newValue.ToString();
logItems.Add(logItem);
}
else if (oldValue == null && newValue == null)
{
continue;
}
}
json = JsonConvert.SerializeObject(logItems);
}
else if (newModel == null && oldModel != null)
{
PropertyInfo[] properties = oldModel.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
object oldValue = item.GetValue(oldModel);
LogItem logItem = new LogItem();
logItem.Property = name;
logItem.Old_Value = oldValue!=null? oldValue.ToString():null;
logItem.New_Value = null;
logItems.Add(logItem);
}
json = JsonConvert.SerializeObject(logItems);
}
return json;
}
public static string CompareModels<T>(List<T> oldModelList, List<T> newModelList) where T : class
{
string json = string.Empty;
if (oldModelList != null && newModelList == null)
{
List<LogModel> list = new List<LogModel>();
for (int i = 0; i < oldModelList.Count; i++)
{
LogModel logModel = new LogModel();
List<LogItem> logItems = new List<LogItem>();
PropertyInfo[] properties = oldModelList[i].GetType().GetProperties();
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
if (name == "ID")
{
logModel.ID = item.GetValue(oldModelList[i]).ToString();
continue;
}
object oldValue = item.GetValue(oldModelList[i]);
LogItem logItem = new LogItem();
logItem.Property = name;
logItem.Old_Value = oldValue != null ? oldValue.ToString() : null;
logItem.New_Value = null;
logItems.Add(logItem);
}
logModel.LogItems=logItems;
list.Add(logModel);
}
json = JsonConvert.SerializeObject(list);
}
return json;
}
public class LogModel
{
public string ID { get; set; }
public List<LogItem> LogItems { get; set; }
}
public class LogItem
{
public string Property { get; set; }
public string Old_Value { get; set; }
public string New_Value { get; set; }
}
}
用法:
oldModel:
var oldModel = MaaSDBContext.Where<模型>($"SELECT * FROM 表名 WHERE ID ='{model.ID}'").FirstOrDefault();
newModel:
var newModel = MaaSDBContext.Where<模型>($"SELECT * FROM 表名 WHERE ID ='{model.ID}'").FirstOrDefault();
差异:
CompareModelHelper.CompareModel<模型>(oldModel, newModel)