/// <summary> /// 将Datatable每行根据列名和对象的属性名关系对应一一赋值 /// </summary> /// <param name="obj"></param> /// <param name="row"></param> /// <param name="cols"></param> public static void ToObject(Object obj, DataRow row, DataColumnCollection cols) { List <string> props = UtilReflection.GetPropertNames(obj); foreach (string prop in props) { if (cols.Contains(prop)) { UtilReflection.SetValue(obj, prop, row[prop].ToString()); } } }
/// <summary> /// 清除数据对象关联的数据对象,一般在获取到所需数据之后最后执行 /// </summary> protected object ClearInclude(object entityObject, bool IsIncludeCommitTime = true, bool IsIncludeUpdateTime = true) { object destObject; if (entityObject.GetType().BaseType.FullName.Equals("System.Object")) { destObject = Activator.CreateInstance(entityObject.GetType()); } else { destObject = Activator.CreateInstance(entityObject.GetType().BaseType); } List <string> keysList = UtilReflection.GetPropertNames(entityObject); PropertyInfo p, p_n; foreach (string key in keysList) { p = entityObject.GetType().GetProperty(key); p_n = destObject.GetType().GetProperty(key); if (p_n != null) { if (p_n.PropertyType.FullName.Contains("Database.")) { p_n.SetValue(destObject, null); } else { if (!IsIncludeCommitTime) { if (key.ToUpper().Equals("COMMITTIME")) { p_n.SetValue(destObject, null); continue; } } if (!IsIncludeUpdateTime) { if (key.ToUpper().Equals("UPDATETIME")) { p_n.SetValue(destObject, null); continue; } } object origin_pro = p.GetValue(entityObject); if (origin_pro != null) { UtilReflection.SetValue(destObject, key, origin_pro.ToString()); } } } } return(destObject); }
/// <summary> /// 清除数据对象关联的数据对象,一般在获取到所需数据之后最后执行 /// </summary> protected object ClearInclude(object entityObject) { object destObject; if (entityObject.GetType().BaseType.FullName.Equals("System.Object")) { destObject = Activator.CreateInstance(entityObject.GetType()); } else { destObject = Activator.CreateInstance(entityObject.GetType().BaseType); } List <string> keysList = UtilReflection.GetPropertNames(entityObject); PropertyInfo p, p_n; foreach (string key in keysList) { p = entityObject.GetType().GetProperty(key); p_n = destObject.GetType().GetProperty(key); if (p_n != null) { if (p_n.PropertyType.FullName.Contains("Database.")) { p_n.SetValue(destObject, null); } else { object origin_pro = p.GetValue(entityObject); if (origin_pro != null) { UtilReflection.SetValue(destObject, key, origin_pro.ToString()); } } } } return(destObject); }