/// <summary> /// convert key-value pair to model /// </summary> /// <typeparam name="T">model type</typeparam> /// <param name="source">key-value pair list</param> /// <returns>model</returns> public static List <T> GetModelList <T>(List <Dictionary <string, object> > source) where T : class { if (source == null || source.Count <= 0) { return(null); } Type type = typeof(T); PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); if (propertyInfos == null || propertyInfos.Length <= 0) { return(null); } List <T> res = new List <T>(); try { foreach (Dictionary <string, object> itemSource in source) { if (itemSource == null || itemSource.Count <= 0) { continue; } foreach (PropertyInfo item in propertyInfos) { T tmp = Activator.CreateInstance <T>(); if (item == null || string.IsNullOrWhiteSpace(item.Name.ToLower()) || !itemSource.Keys.Contains(item.Name.ToLower())) { continue; } SetPropertyValue(item, tmp, itemSource[item.Name.ToLower()]); if (!res.Contains(tmp) && !res.Equals(Activator.CreateInstance <T>())) { res.Add(tmp); } } } } catch (Exception ex) { LogTool.Error(ex); return(null); } return(res.Count <= 0 ? null : res); }
/// <summary> /// set cache /// </summary> /// <param name="key">cache key</param> /// <param name="value">cache value</param> /// <param name="minutes">cache expiration</param> /// <returns>set result</returns> public static bool SetCache(string key, object value, int?minutes = 20) { try { RemoveCache(key); if (minutes == null) { CacheEntity.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration); } else { CacheEntity.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, minutes.Value, 0)); } return(true); } catch (Exception ex) { LogTool.Error(ex); return(false); } }
/// <summary> /// compare model property value equal or not /// </summary> /// <typeparam name="T">model type</typeparam> /// <param name="a">value 1</param> /// <param name="b">value 2</param> /// <returns>compare result</returns> public static bool ACompareB <T>(T a, T b) where T : class { if (a == null && b == null) { return(true); } if ((a == null && b != null) || (a != null && b == null)) { return(false); } Type type = typeof(T); PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); if (propertyInfos == null || propertyInfos.Length <= 0) { return(false); } try { foreach (PropertyInfo pi in propertyInfos) { object tmpA = pi.GetValue(a, null); object tmpB = pi.GetValue(b, null); if (CompareValue(tmpA, tmpB, pi)) { continue; } else { return(false); } } return(true); } catch (Exception ex) { LogTool.Error(ex); return(false); } }
/// <summary> /// get node value string by Xml xpath with out check /// </summary> /// <param name="xpath">xpath string</param> /// <param name="targetXml">xml file full path or xml content</param> /// <returns>node value string</returns> private static string GetValueByXPath(string xpath, string targetXml) { string res = string.Empty; XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(targetXml); if (xmlDoc == null) { xmlDoc.LoadXml(targetXml); if (xmlDoc == null) { res = string.Empty; } } XmlNode node = xmlDoc.SelectSingleNode(xpath); if (node == null) { res = string.Empty; } string[] tmp = node.InnerText.Split(new char[] { '\t', '\r', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (tmp != null && tmp.Length > 0) { res = string.Join(" ", tmp); } else { res = null; } } catch (Exception exLoad) { LogTool.Error(exLoad); } return(res); }
/// <summary> /// set the node value by xml xpath /// </summary> /// <param name="xpath">xpath string</param> /// <param name="nodeValue">xml node value</param> /// <param name="xmlFullPath">xml file path</param> /// <param name="useCache">is use the cache</param> public static void SetNodeValueByXPath(string xpath, string nodeValue, string xmlFullPath = null, bool useCache = true) { if (string.IsNullOrEmpty(xpath)) { LogTool.Info("xpath is not defined"); return; } string xmlPath = string.IsNullOrEmpty(xmlFullPath.Trim()) ? _defaultXmlPath : xmlFullPath; if (!File.Exists(xmlPath)) { LogTool.Info("xml is not exist"); return; } if (useCache) { StringBuilder sbCombine = new StringBuilder(); sbCombine.Append(xpath); sbCombine.Append(xmlFullPath); string cacheKey = sbCombine.ToString().GetHashCode().ToString(); CacheTool.SetCache(cacheKey, nodeValue, 30); } SetValueByXPath(xpath, xmlPath, nodeValue); }
/// <summary> /// object convert to string /// </summary> /// <param name="obj">object</param> /// <param name="timeFormat">datetime format string</param> /// <returns>string</returns> public static string GetString(object obj, string timeFormat = null) { if (obj == null || obj == DBNull.Value) { return(null); } DateTime?tmpDatetime = obj as DateTime?; if (string.IsNullOrWhiteSpace(timeFormat)) { timeFormat = "yyyy-MM-dd HH:mm:ss.fff"; } if (tmpDatetime.HasValue) { if (string.IsNullOrWhiteSpace(timeFormat.Trim())) { return(tmpDatetime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff")); } else { try { return(tmpDatetime.Value.ToString(timeFormat)); } catch (Exception ex) { LogTool.Error(ex); return(tmpDatetime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff")); } } } else { return(obj.ToString()); } }