当前位置:
首页 > temp > 简明python教程 >
-
如何两个对象数据比对
public partial class ModelStatusDictionary<T> where T : new() { static readonly DynamicMethod<T> _dynamicMethod = new DynamicMethod<T>(); /// <summary> /// 判断指定特性的属性 /// </summary> /// <param name="obj">对象</param> /// <param name="NonStance">是否获取基类的属性</param> /// <param name="NonProperty">排除的属性</param> /// <returns></returns> public static (bool state, string message) ModelVerified(T obj, bool NonStance = true, ICollection<string> NonProperty = null) { bool isEmpty = true; string msge = string.Empty; var ps = typeof(T).GetProperties(); if (!NonStance) ps = typeof(T).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); if (NonProperty != null) ps = ps.Where(x => !NonProperty.Contains(x.Name)).ToArray(); var vModel = (from p in ps where p.GetCustomAttribute<DisplayNameAttribute>(true) != null select new { name = p.Name, dname = p.GetCustomAttribute<DisplayNameAttribute>(true).DisplayName, velue = _dynamicMethod.GetValue(obj, p.Name), }).Distinct().ToList(); var fiel = vModel.Where(x => !Isfield(x.velue)).ToList(); if (fiel.Count > 0) { isEmpty = false; msge = $"{string.Join(",", fiel.Select(x => x.dname).ToList())},Required fields cannot be empty!"; } return (isEmpty, msge); } /// <summary> /// 两个类型一样的对象做比较 /// </summary> /// <param name="model">A</param> /// <param name="modelData">B</param> public static (bool state, List<ModelEqualDictionary> dictionary) ModelEquals(T lastModel, T newModel) { bool isEquals = true; var ps = typeof(T).GetProperties(); var _last = GetObject(ps, lastModel);// (from p in ps var _new = GetObject(ps, newModel); var fields = _new.Except(_last).Where(x => !string.IsNullOrWhiteSpace(x.dname)).Select(x => new ModelEqualDictionary() { DisplayName = x.dname, LostValue = _last.Where(y => y.name == x.name).FirstOrDefault().value, NewValue = x.value }).ToList(); if (fields != null) if (fields.Any()) isEquals = false; return (isEquals, fields); } static IEnumerable<dynamic> GetObject(PropertyInfo[] property, object model) { if (model == null) { model = new T(); } var dy = (from p in property where p.GetCustomAttribute<DisplayNameAttribute>(true) != null select new { name = p.Name, dname = p.GetCustomAttribute<DisplayNameAttribute>(true).DisplayName, value = GetModelValue(model, p), }); return dy; } /// <summary> /// 两个类型一样的对象做比较 /// </summary> /// <param name="model">A</param> /// <param name="modelData">B</param> /// <param name="noProperty">C</param> public static (bool state, List<ModelEqualDictionary> dictionary) ModelEqual(T lastModel, T newModel,bool nonProperty = false,ICollection<string> NonProperty = null) { bool isEquals = true; var ps = typeof(T).GetProperties(); if (NonProperty != null) { ps = ps.Where(x => !NonProperty.Contains(x.Name)).ToArray(); } var _last = nonProperty ? GetNonPropertyModel(ps,lastModel): GetModel(ps, lastModel); var _new = nonProperty ? GetNonPropertyModel(ps, newModel) : GetModel(ps, newModel); var fields = _new.Except(_last).Select(x => new ModelEqualDictionary() { DisplayName = x.dname, LostValue = _last.Where(y => y.name == x.name).FirstOrDefault().value, NewValue = x.value }).ToList(); if (fields != null) if (fields.Any()) isEquals = false; return (isEquals, fields); } static IEnumerable<dynamic> GetNonPropertyModel(PropertyInfo[] property, object model) { var a = property.Where(p => p.GetCustomAttribute<IsRequiredAttribute>(true) != null).ToList(); var dy = (from p in property where p.GetCustomAttribute<IsRequiredAttribute>(true) != null select new { name = p.Name, dname = p.GetCustomAttribute<IsRequiredAttribute>(true).IsRequired, value = GetModelValue(model, p), }); return dy; } static IEnumerable<dynamic> GetModel(PropertyInfo[] property, object model) { var a = property.Where(p => p.GetCustomAttribute<IsRequiredAttribute>(true) != null).ToList(); var dy = (from p in property where p.GetCustomAttribute<IsRequiredAttribute>(true) != null select new { name = p.Name, dname = p.GetCustomAttribute<IsRequiredAttribute>(true).IsRequired, value = GetModelValue(model, p), }); return dy; } static object GetModelValue(object obj, PropertyInfo property) { object value = string.Empty; if (getPropertyType(property.PropertyType) == typeof(Nullable)) { var nextObj = obj.GetType().GetProperty(property.Name).GetValue(obj, null); if (property.PropertyType.BaseType.Name == "Enum") { value = nextObj; } else if (nextObj != null) { if (getPropertyType(nextObj.GetType()) == typeof(DateTime)) { value = nextObj; } else { var ps = nextObj.GetType().GetProperties(); var thisObj = GetObject(ps, nextObj); value = string.Join(",", thisObj ?? thisObj.Select(x => x.value).ToList()); } } } else { value = obj.GetType().GetProperty(property.Name).GetValue(obj, null); } return value; } static bool Isfield(object value) { if (value == null) return false; var val = value.GetType().ToString(); switch (val) { case "System.Boolean": return value == null ? false : true; case "System.Byte": return value == null ? false : true; case "System.SByte": return value == null ? false : true; case "System.Char": return string.IsNullOrWhiteSpace(value.ToString()) ? false : true; case "System.Decimal": return value == null ? false : true; case "System.Double": return value == null ? false : true; case "System.Single": return value == null ? false : true; case "System.Int32": return value == null ? false : true; case "System.UInt32": return value == null ? false : true; case "System.Int64": return value == null ? false : true; case "System.UInt64": return value == null ? false : true; case "System.Object": return value == null ? false : true; case "System.Int16": return value == null ? false : true; case "System.UInt16": return value == null ? false : true; case "System.String": return string.IsNullOrWhiteSpace(value.ToString()) ? false : true; case "System.DateTime.Date": return value == null ? false : true; case "System.DateTime": return value == null ? false : true; case "System.Guid": return value == null ? false : true; default: return true; } } static Type getPropertyType(Type obj) { if (obj == null) return typeof(DBNull); var val = obj.ToString(); switch (val) { case "System.Boolean": return typeof(Boolean); case "System.Byte": return typeof(Byte); case "System.SByte": return typeof(SByte); case "System.Char": return typeof(char); case "System.Decimal": return typeof(Decimal); case "System.Double": return typeof(Double); case "System.Single": return typeof(Single); case "System.Int32": return typeof(Int32); case "System.UInt32": return typeof(UInt32); case "System.Int64": return typeof(Int64); case "System.UInt64": return typeof(UInt64); case "System.Object": return typeof(Object); case "System.Int16": return typeof(Int16); case "System.UInt16": return typeof(UInt16); case "System.String": return typeof(string); case "System.DateTime.Date": return typeof(DateTime); case "System.DateTime": return typeof(DateTime); case "System.Guid": return typeof(Guid); case "System.Enum": return typeof(Enum); default: return typeof(Nullable); } } } public class ModelEqualDictionary { public string DisplayName { get; set; } public object LostValue { get; set; } public object NewValue { get; set; } }
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数