/// <summary> /// 获取实体全部操作列 /// </summary> /// <param name="t"></param> /// <returns></returns> public static List <string> GetAllColumns(T t) { List <string> allcolumns = new List <string>(); object obj = UtilitysForT <T> .GetObj(t); PropertyInfo[] fields = obj.GetType().GetProperties(); foreach (PropertyInfo ts in fields) { allcolumns.Add(ts.Name.ToUpper()); } return(allcolumns); }
/// <summary> /// 设置实体操作位标识 /// </summary> /// <param name="t"></param> /// <param name="opflag">增I 改M 删除D</param> public static void SetObjOpFlag(T t, string opflag) { PropertyInfo[] fields = UtilitysForT <T> .GetObjPropertyInfo(t);//获取指定对象的所有公共属性 object obj = UtilitysForT <T> .GetObj(t); foreach (PropertyInfo ts in fields) { if (ts.Name.ToUpper() == "OpFlag".ToUpper()) { ts.SetValue(obj, opflag.ToUpper(), null); break; } } }
/// <summary> /// 获取泛型集合主键对应的值(多个主键时,直接拼接在一起) /// </summary> /// <param name="soc"></param> /// <param name="mainProperty"></param> /// <returns></returns> public static string GetMainPropertyValue(T soc, List <string> mainProperty) { PropertyInfo[] fields = UtilitysForT <T> .GetObjPropertyInfo(soc);//获取指定对象的所有公共属性 string mainValue = ""; object obj = UtilitysForT <T> .GetObj(soc); //获取主键值 foreach (var item in mainProperty) { foreach (PropertyInfo t in fields) { if (t.Name.ToUpper() == item.ToUpper()) { mainValue = mainValue + (t.GetValue(obj, null) == null ? "" : t.GetValue(obj, null).ToString()); break; } } } return(mainValue); }
/// <summary> /// 获取对象属性数组 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static PropertyInfo[] GetObjPropertyInfo(T t) { object obj = UtilitysForT <T> .GetObj(t); return(obj.GetType().GetProperties());//获取指定对象的所有公共属性 }
/// <summary> /// 对比两个泛型,处理操作标志位 增I 改M 删D /// </summary> /// <param name="soc">当前目标</param> /// <param name="oldSoc">历史目标</param> /// <param name="mainProperty">主键属性名</param> /// <returns></returns> public static List <T> DueWithListT(List <T> soc, List <T> oldSoc, List <string> mainProperty) { List <T> reslist = new List <T>(); object obj1 = null; object obj2 = null; PropertyInfo[] fields = UtilitysForT <T> .GetObjPropertyInfo(soc[0]); //获取指定对象的所有公共属性 string mainValue = ""; //主键值 string mainValueOld = ""; //历史目标主键值 bool matchSuccess = false; //匹配成功 //正向比较,处理I M foreach (var item1 in soc) { matchSuccess = false; obj1 = UtilitysForT <T> .GetObj(item1); mainValue = UtilitysForT <T> .GetMainPropertyValue(item1, mainProperty); //查找历史目录主键 foreach (var item2 in oldSoc) { obj2 = UtilitysForT <T> .GetObj(item2); mainValueOld = UtilitysForT <T> .GetMainPropertyValue(item2, mainProperty); if (mainValue == mainValueOld) { matchSuccess = true; break; } } if (matchSuccess) { //匹配上, 再比较是否有变化,有是修改,没有忽略 if (!UtilitysForT <T> .CompareObjectValue(obj1, obj2)) { UtilitysForT <T> .SetObjOpFlag(item1, "M"); reslist.Add(item1); } } else { //没有匹配上,是新增 UtilitysForT <T> .SetObjOpFlag(item1, "I"); reslist.Add(item1); } } //反向比较,处理D foreach (var item1 in oldSoc) { matchSuccess = false; obj1 = UtilitysForT <T> .GetObj(item1); mainValue = UtilitysForT <T> .GetMainPropertyValue(item1, mainProperty); //查找历史目录主键 foreach (var item2 in soc) { obj2 = UtilitysForT <T> .GetObj(item2); mainValueOld = UtilitysForT <T> .GetMainPropertyValue(item2, mainProperty); if (mainValue == mainValueOld) { matchSuccess = true; break; } } if (matchSuccess) { //匹配上, 上次已经处理,这次忽略 } else { //没有匹配上,是删除 UtilitysForT <T> .SetObjOpFlag(item1, "D"); reslist.Add(item1); } } return(reslist); }