/// <summary> /// 对象转换到日志Json - 其它 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj">对象</param> /// <returns></returns> public static string ConvertToJson <T>(T obj) where T : class { try { if (obj == null) { return(string.Empty); } List <OtherLogInfo> result = new List <OtherLogInfo>(); Type type = obj.GetType(); foreach (PropertyInfo item in type.GetProperties()) { DescriptionAttribute attribute = item.GetCustomAttribute <DescriptionAttribute>(); if (attribute != null) { OtherLogInfo info = new OtherLogInfo(); info.FiledName = item.Name; info.FiledCnName = attribute.Description; info.Value = GetFormatValue(item, obj); result.Add(info); } } if (result.Count > 0) { return(JsonConvert.SerializeObject(result)); } } catch { } return(string.Empty); }
/// <summary> /// 批量操作日志类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="obj2"></param> /// <returns></returns> public static string ConvertToJson <T>(List <T> obj, List <T> obj2) where T : class { try { if (obj == null || obj2 == null || obj.Count != obj2.Count) { return(string.Empty); } Type type = typeof(T); int changeCount = 0; List <List <object> > result = new List <List <object> >(); for (int i = 0; i < obj.Count; i++) { List <object> temp = new List <object>(); foreach (PropertyInfo item in type.GetProperties()) { DescriptionAttribute attribute = item.GetCustomAttribute <DescriptionAttribute>(); if (attribute == null) { continue; } IsChangeFlagAttribute changeAttribute = item.GetCustomAttribute <IsChangeFlagAttribute>(); if (changeAttribute == null) { UpdateLogInfo info = new UpdateLogInfo(); info.FiledName = item.Name; info.FiledCnName = attribute.Description; info.OldValue = GetFormatValue(item, obj[i]); info.NewValue = GetFormatValue(item, obj2[i]); if (info.OldValue.ToString() != info.NewValue.ToString()) { changeCount++; temp.Add(info); } } else { OtherLogInfo info = new OtherLogInfo(); info.FiledName = item.Name; info.FiledCnName = attribute.Description; info.Value = GetFormatValue(item, obj[i]); temp.Add(info); } } if (temp.Count > 0) { result.Add(temp); } } if (changeCount > 0) { return(JsonConvert.SerializeObject(result)); } } catch { } return(string.Empty); }
/// <summary> /// List对象转换到日志Json - 其它 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj">对象</param> /// <returns></returns> public static string ConvertToJson <T>(List <T> obj) where T : class { try { if (obj == null || obj.Count == 0) { return(string.Empty); } Type type = typeof(T); List <List <OtherLogInfo> > result = new List <List <OtherLogInfo> >(); for (int i = 0; i < obj.Count; i++) { List <OtherLogInfo> temp = new List <OtherLogInfo>(); foreach (PropertyInfo item in type.GetProperties()) { DescriptionAttribute attribute = item.GetCustomAttribute <DescriptionAttribute>(); if (attribute != null) { OtherLogInfo info = new OtherLogInfo(); info.FiledName = item.Name; info.FiledCnName = attribute.Description; info.Value = GetFormatValue(item, obj[i]); temp.Add(info); } } if (temp.Count > 0) { result.Add(temp); } } if (result.Count > 0) { return(JsonConvert.SerializeObject(result)); } } catch { } return(string.Empty); }