public static DataTable GetToDataTable(this List <BulkCopyModel> collection, Type type, string target) { var props = type.GetProperties(); string tableName = CoreUtils.GetTableName(type); string sqlDataColumns = "SElect TOP 1 * FROM " + tableName + " WHERE 1=0"; DataTable dtColumns = new DataTable(); dtColumns = db.FindQuery(sqlDataColumns, null, target).Find(); if (dtColumns == null) { return(null); } dtColumns.TableName = tableName; if (collection.Count > 0) { foreach (var item in collection) { DataRow dr = dtColumns.NewRow(); foreach (DataColumn column in dtColumns.Columns) { var propx = props.Where(p => p.Name == column.ColumnName).ToList(); if (propx != null && propx.Count > 0) { var px = propx.First(); object obj = px.GetValue(item.CopyModel, null); if (obj != null) { dr[column.ColumnName] = obj; } else { dr[column.ColumnName] = DBNull.Value; } } } dtColumns.Rows.Add(dr); } } return(dtColumns); }
/// <summary> /// 获取两个对象间的值发生变化的描述 /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="obj1">变化前的对象</param> /// <param name="obj2">变化后的对象</param> /// <param name="isDes">是否过滤掉没有[Description]标记的</param> /// <returns>字符串</returns> internal static List <ChangedInfo> GetChangeInfos <T>(T obj1, T obj2, HashSet <string> propertyChangedLis) where T : new() { string res = string.Empty; List <ChangedInfo> lis = new List <ChangedInfo>(); if (obj1 == null || obj2 == null) { return(new List <ChangedInfo>()); } if (propertyChangedLis == null || propertyChangedLis.Count() <= 0) { return(new List <ChangedInfo>()); } var properties = from property in (typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)) select property; //string objVal1 = string.Empty; //string objVal2 = string.Empty; foreach (var propertyName in propertyChangedLis) { var property = properties.Where(p => p.Name == propertyName).FirstOrDefault(); var objVal1 = property.GetValue(obj1, null) == null ? string.Empty : property.GetValue(obj1, null); var objVal2 = property.GetValue(obj2, null) == null ? string.Empty : property.GetValue(obj2, null); var keyFiledName = CoreUtils.GetKeyFiledName(typeof(T)); var tableName = CoreUtils.GetTableName(typeof(T)); var keyVlaue = CoreUtils.GetObjectPropertyValue(obj1, keyFiledName).ToSafeString(""); string des = string.Empty; //DescriptionAttribute descriptionAttribute = ((DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute))); //if (descriptionAttribute != null) //{ // des = ((DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute))).Description;// 属性值 //} //if (isDes && descriptionAttribute == null) //{ // continue; //} if (objVal1 == null && objVal2 == null) { continue; } if ((objVal1 == null && objVal2 != null) || (objVal1 != null && objVal2 == null)) { var dispalyname = ""; object[] attrs = property.GetCustomAttributes(true); var f = (from p in attrs where p.GetType().Name == "DisplayAttribute" select p).FirstOrDefault(); if (f != null) { DisplayAttribute displayAttr = f as System.ComponentModel.DataAnnotations.DisplayAttribute; dispalyname = displayAttr.Name; } lis.Add(new ChangedInfo() { FFiledName = property.Name, FNewValue = objVal2.ToSafeString(), FOldValue = objVal1.ToSafeString(), FOrgType = property.GetType().ToString(), FFiledDes = dispalyname, FTableName = tableName, FKeyFiledName = keyFiledName, FKeyValue = keyVlaue }); continue; } if (!(objVal1.Equals(objVal2))) { var dispalyname = ""; object[] attrs = property.GetCustomAttributes(true); var f = (from p in attrs where p.GetType().Name == "DisplayAttribute" select p).FirstOrDefault(); if (f != null) { DisplayAttribute displayAttr = f as System.ComponentModel.DataAnnotations.DisplayAttribute; dispalyname = displayAttr.Name; } lis.Add(new ChangedInfo() { FFiledName = property.Name, FNewValue = objVal2.ToSafeString(), FOldValue = objVal1.ToSafeString(), FOrgType = property.GetType().ToString(), FFiledDes = dispalyname, FTableName = tableName, FKeyFiledName = keyFiledName, FKeyValue = keyVlaue }); } } return(lis); }