• C# 实体/集合差异比较,比较两个实体或集合值是否一样,将实体2的值动态赋值给实体1(名称一样的属性进行赋值)


     1         /// <summary>
     2         /// 实体差异比较器
     3         /// </summary>
     4         /// <param name="source">源版本实体</param>
     5         /// <param name="current">当前版本实体</param>
     6         /// <returns>true 存在变更 false 未变更</returns>
     7         protected static bool DifferenceComparison<T1, T2>(T1 source, T2 current,List<string> exclude) where T1 : MBase where T2 : MBase
     8         {
     9             Type t1 = source.GetType();
    10             Type t2 = current.GetType();
    11             PropertyInfo[] property1 = t1.GetProperties();
    12             //排除主键和基础字段
    13             //List<string> exclude = new List<string>() { "Id", "InsertTime", "UpdateTime", "DeleteTime", "Mark", "Version", "Code" };
    14             foreach (PropertyInfo p in property1)
    15             {
    16                 string name = p.Name;
    17                 if (exclude.Contains(name)) { continue; }
    18                 string value1 = p.GetValue(source, null)?.ToString();
    19                 string value2 = t2.GetProperty(name)?.GetValue(current, null)?.ToString();
    20                 if (value1 != value2)
    21                 {
    22                     return true;
    23                 }
    24             }
    25             return false;
    26         }
    27         /// <summary>
    28         /// 集合差异比较器,比较两个实体集合值是否一样
    29         /// </summary>
    30         /// <param name="source">源版本实体集合</param>
    31         /// <param name="current">当前版本实体集合</param>
    32         /// <returns>true 存在变更 false 未变更</returns>
    33         protected static bool DifferenceComparison<T1, T2>(List<T1> source, List<T2> current) where T1 : MBase where T2 : MBase
    34         {
    35             if (source.Count != current.Count) { return true; }
    36             for (int i = 0; i < source.Count; i++)
    37             {
    38                 bool flag = DifferenceComparison(source[i], current[i]);
    39                 if (flag) { return flag; }
    40             }
    41             return false;
    42         }
    43         /// <summary>
    44         /// 将实体2的值动态赋值给实体1(名称一样的属性进行赋值)
    45         /// </summary>
    46         /// <param name="model1">实体1</param>
    47         /// <param name="model2">实体2</param>
    48         /// <returns>赋值后的model1</returns>
    49         protected static T1 BindModelValue<T1, T2>(T1 model1, T2 model2) where T1 : class where T2 : class
    50         {
    51             Type t1 = model1.GetType();
    52             Type t2 = model2.GetType();
    53             PropertyInfo[] property2 = t2.GetProperties();
    54             //排除主键
    55             List<string> exclude = new List<string>() { "Id" };
    56             foreach (PropertyInfo p in property2)
    57             {
    58                 if (exclude.Contains(p.Name)) { continue; }
    59                 t1.GetProperty(p.Name)?.SetValue(model1, p.GetValue(model2, null));
    60             }
    61             return model1;
    62         }
    List<string> exclude用于排除实体中的字段,这些字段将不参与比较

  • 相关阅读:
    MySQL 中事务的实现
    Spring Boot 生成接口文档 swagger2
    一些开源项目
    LTP随笔——本地调用ltp之ltp4j
    Linux随笔之——./configure、make、make install(转)
    Linux笔记之——Linux关机命令详解(转)
    Ubuntu杂记之——JDK、ANT安装、配置
    Android随笔之——Android ADB详解
    Android APK签名
    Android随笔之——跨进程通信(一) Activity篇
  • 原文地址:https://www.cnblogs.com/codelir/p/6856194.html
Copyright © 2020-2023  润新知