using System; using System.Collections.Generic; using System.Data; using System.Dynamic; using System.IO; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; namespace DeviceRepair.Utils { public static class ObjectExtend { /// /// 对比两个对象并获取差异 /// /// 比较的对象 /// 比较的对象2,字典集合的值将会为该对象中的值 /// public static Dictionary GetDifferencesColumn(this object t1, object t2) { if (t1.GetType() != t2.GetType()) throw new ArgumentException("实体对象必须是相同的."); var differences = new Dictionary(); var properties = t1.GetType().GetProperties(); foreach (var prop in properties) { var value1 = prop.GetValue(t1); var value2 = prop.GetValue(t2); if (!Equals(value1, value2)) { differences.Add(prop.Name, value2); } } return differences; } /// /// 获取对象中不同的值,取出赋值到新对象 /// /// /// /// /// public static T GetDifferencesModel(this T t1, T t2) where T : class { PropertyInfo[] properties = typeof(T).GetProperties(); T newObj = Activator.CreateInstance(); foreach (PropertyInfo property in properties) { object value1 = property.GetValue(t1); object value2 = property.GetValue(t2); if (!Equals(value1, value2)) // 判断两个值是否相等 { property.SetValue(newObj, value2); // 将不同的值设置到新对象中 } } return newObj; } /// /// 数据集转键值对 /// /// /// public static Dictionary toDictionary(this byte[] byteArray) { Dictionary deserializedDictionary; using (MemoryStream memoryStream = new MemoryStream(byteArray)) { BinaryFormatter binaryFormatter = new BinaryFormatter(); deserializedDictionary = (Dictionary)binaryFormatter.Deserialize(memoryStream); } return deserializedDictionary; } public static T ToObject(this byte[] byteArray) { BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream stream = new MemoryStream(byteArray)) { object obj = formatter.Deserialize(stream); return (T)obj; } } /// /// 赋值对象 /// /// /// /// public static void Copy(object source, object destination) { var properties = typeof(T).GetProperties(); foreach (var property in properties) { if (property.CanRead && property.CanWrite) { var value = property.GetValue(source); property.SetValue(destination, value); } } } public static void CopyPropertiesTo(this T source, T target) { if (source == null) throw new ArgumentNullException(nameof(source)); if (target == null) throw new ArgumentNullException(nameof(target)); PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo property in properties) { if (property.CanWrite) { property.SetValue(target, property.GetValue(source), null); } } } /// /// 反射实现两个类的对象之间相同属性的值的复制 /// 适用于初始化新实体 /// /// 返回的实体 /// 数据源实体 /// 数据源实体 /// 返回的新实体 public static D Mapper(S s) { D d = Activator.CreateInstance(); //构造新实例 try { var Types = s.GetType(); //获得类型 var Typed = typeof(D); foreach (PropertyInfo sp in Types.GetProperties()) //获得类型的属性字段 { foreach (PropertyInfo dp in Typed.GetProperties()) { if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType && dp.Name != "Error" && dp.Name != "Item") //判断属性名是否相同 { dp.SetValue(d, sp.GetValue(s, null), null); //获得s对象属性的值复制给d对象的属性 } } } } catch (Exception ex) { throw ex; } return d; } public static bool IsNull(this DataSet dsDatas) { return dsDatas == null || dsDatas.Tables.Count == 0; } public static bool IsNull(this List lsData) { return lsData == null || lsData.Count == 0; } public static bool IsNull(this IList lsData) { return lsData == null || lsData.Count == 0; } public static bool IsNull(this DataTable dtData) { return dtData == null || dtData.Rows.Count == 0; } } }