CommonHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Converters;
  6. namespace ShwasherSys
  7. {
  8. public static class CommonHelper
  9. {
  10. public static string Obj2String(this object obj)
  11. {
  12. try
  13. {
  14. return JsonConvert.SerializeObject(obj);
  15. }
  16. catch (Exception e)
  17. {
  18. typeof(CommonHelper).LogError(e);
  19. return "";
  20. }
  21. }
  22. public static T GetResultModel<T>(this string objStr, bool isLower = true)
  23. {
  24. try
  25. {
  26. var converter = new ExpandoObjectConverter();
  27. dynamic jsonObject = JsonConvert.DeserializeObject<ExpandoObject>(objStr, converter);
  28. if (isLower)
  29. {
  30. if (jsonObject.success)
  31. {
  32. T info = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(jsonObject.result));
  33. return info;
  34. }
  35. }
  36. else
  37. {
  38. if (jsonObject.Success)
  39. {
  40. T info = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(jsonObject.Result));
  41. return info;
  42. }
  43. }
  44. }
  45. catch (Exception e)
  46. {
  47. typeof(CommonHelper).LogError(e);
  48. }
  49. return default(T);
  50. }
  51. public static T GetModel<T>(this string source)
  52. {
  53. try
  54. {
  55. if (string.IsNullOrEmpty(source))
  56. {
  57. return default(T);
  58. }
  59. return JsonConvert.DeserializeObject<T>(source);
  60. }
  61. catch (Exception e)
  62. {
  63. typeof(CommonHelper).LogError(e);
  64. return default(T);
  65. }
  66. }
  67. public static T GetObj<T>(this string source, string key = null)
  68. {
  69. try
  70. {
  71. var converter = new ExpandoObjectConverter();
  72. var jsonObject = JsonConvert.DeserializeObject<ExpandoObject>(source, converter);
  73. if (string.IsNullOrEmpty(key))
  74. {
  75. T info = JsonConvert.DeserializeObject<T>(source);
  76. return info;
  77. }
  78. object obj = jsonObject.GetValue(key);
  79. return (T)obj;
  80. }
  81. catch (Exception e)
  82. {
  83. typeof(CommonHelper).LogError(e);
  84. return default(T);
  85. }
  86. }
  87. /// <summary>
  88. /// Gets the value.
  89. /// </summary>
  90. /// <param name="objs">The object.</param>
  91. /// <param name="key">The key.</param>
  92. /// <returns>System.Object.</returns>
  93. public static dynamic GetValue(this object objs, string key)
  94. {
  95. if (objs is List<object> oos)
  96. {
  97. foreach (var oo in oos)
  98. {
  99. var result = GetValue(oo, key);//递归返回匹配的值
  100. if (result != null)
  101. return result;
  102. }
  103. }
  104. else if (objs is object[] objects)
  105. {
  106. foreach (var oo in objects)
  107. {
  108. var result = GetValue(oo, key);//递归返回匹配的值
  109. if (result != null)
  110. return result;
  111. }
  112. }
  113. else if (objs is IDictionary<string, object> dictionarys)
  114. {
  115. foreach (var dic in dictionarys)
  116. {
  117. if (dic.Key == key || dic.Key == key.ToLower() || dic.Key == key.ToUpper())
  118. return dic.Value;
  119. }
  120. //如果上面的遍历没有结果,则该值可能嵌套在property.Value里面,需要递归解析
  121. foreach (var dic in dictionarys)
  122. {
  123. var result = GetValue(dic.Value, key);//递归返回匹配的值
  124. if (result != null)
  125. return result;
  126. }
  127. }
  128. return null;//没有匹配值,返回null
  129. }
  130. public static int ToInt(this System.Enum e)
  131. {
  132. return e.GetHashCode();
  133. }
  134. public static IEnumerable<TSource> DistinctBy<TSource, TKey>
  135. (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  136. {
  137. HashSet<TKey> seenKeys = new HashSet<TKey>();
  138. foreach (TSource element in source)
  139. {
  140. if (seenKeys.Add(keySelector(element)))
  141. {
  142. yield return element;
  143. }
  144. }
  145. }
  146. }
  147. }