C#:俩个对象找茬

 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值