CommonExtensions.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace SqlSugar
  10. {
  11. public static class CommonExtensions
  12. {
  13. public static string GetNonNegativeHashCodeString(this string input)
  14. {
  15. // 获取哈希码,然后取绝对值,转换为字符串
  16. string hashCode = "hs"+Math.Abs(input.GetHashCode());
  17. return hashCode;
  18. }
  19. public static string SafeSubstring(this string str, int startIndex, int length)
  20. {
  21. if (str == null)
  22. {
  23. throw new ArgumentNullException(nameof(str));
  24. }
  25. if (startIndex < 0)
  26. {
  27. startIndex = 0;
  28. }
  29. if (startIndex >= str.Length)
  30. {
  31. return string.Empty; // 返回空字符串,因为起始索引超过字符串长度
  32. }
  33. if (length < 0)
  34. {
  35. length = 0;
  36. }
  37. // 截取字符串时,确保不超过字符串的长度
  38. return str.Substring(startIndex, Math.Min(length, str.Length - startIndex));
  39. }
  40. public static Dictionary<string, object> ToDictionary<T>(this List<T> list, string keyPropertyName, string valuePropertyName)
  41. {
  42. var keyProperty = typeof(T).GetProperty(keyPropertyName);
  43. var valueProperty = typeof(T).GetProperty(valuePropertyName);
  44. return list.ToDictionary(
  45. item => keyProperty.GetValue(item)+"",
  46. item => valueProperty.GetValue(item)
  47. );
  48. }
  49. public static MethodInfo GetMyMethod(this Type type,string name, int argCount)
  50. {
  51. return type.GetMethods().FirstOrDefault(it => it.Name == name && it.GetParameters().Length == argCount);
  52. }
  53. public static MethodInfo GetMyMethod(this Type type, string name, int argCount,Type parameterType)
  54. {
  55. return type.GetMethods().FirstOrDefault(it =>
  56. it.Name == name &&
  57. it.GetParameters().Length == argCount&&
  58. it.GetParameters().First().ParameterType==parameterType);
  59. }
  60. public static MethodInfo GetMyMethod(this Type type, string name, int argCount, bool isList)
  61. {
  62. var methods= type.GetMethods().Where(it => it.Name == name).Where(it =>
  63. it.GetParameters().Length == argCount&&
  64. it.GetParameters()[0].ToString().Contains("List`") == isList).ToList();
  65. return methods.First();
  66. }
  67. public static MethodInfo GetMyMethod(this Type type, string name, int argCount, Type parameterType,Type parameterType2)
  68. {
  69. return type.GetMethods().Where(it=>it.Name == name).FirstOrDefault(it =>
  70. it.GetParameters().Length == argCount &&
  71. it.GetParameters().First().ParameterType == parameterType&&
  72. it.GetParameters()[1].ParameterType == parameterType2) ;
  73. }
  74. public static MethodInfo GetMyMethodNoGen(this Type type, string name, int argCount, Type parameterType, Type parameterType2)
  75. {
  76. return type.GetMethods().Where(it => it.Name == name&&it?.GetGenericArguments()?.Count()==0).FirstOrDefault(it =>
  77. it.GetParameters().Length == argCount &&
  78. it.GetParameters().First().ParameterType == parameterType &&
  79. it.GetParameters()[1].ParameterType == parameterType2);
  80. }
  81. public static MethodInfo GetMyMethod(this Type type, string name, int argCount, Type parameterType, Type parameterType2, Type parameterType3)
  82. {
  83. return type.GetMethods().Where(it => it.Name == name).FirstOrDefault(it =>
  84. it.GetParameters().Length == argCount &&
  85. it.GetParameters().First().ParameterType == parameterType &&
  86. it.GetParameters()[1].ParameterType == parameterType2&&
  87. it.GetParameters()[2].ParameterType == parameterType3);
  88. }
  89. public static MethodInfo GetMyMethod(this Type type, string name, int argCount, Type parameterType, Type parameterType2, Type parameterType3,Type parameterType4)
  90. {
  91. return type.GetMethods().Where(it => it.Name == name).FirstOrDefault(it =>
  92. it.GetParameters().Length == argCount &&
  93. it.GetParameters().First().ParameterType == parameterType &&
  94. it.GetParameters()[1].ParameterType == parameterType2 &&
  95. it.GetParameters()[2].ParameterType == parameterType3&&
  96. it.GetParameters()[3].ParameterType == parameterType4);
  97. }
  98. public static MethodInfo GetMyMethod(this Type type, string name, int argCount, Type parameterType, Type parameterType2, Type parameterType3, Type parameterType4, Type parameterType5)
  99. {
  100. return type.GetMethods().Where(it => it.Name == name).FirstOrDefault(it =>
  101. it.GetParameters().Length == argCount &&
  102. it.GetParameters().First().ParameterType == parameterType &&
  103. it.GetParameters()[1].ParameterType == parameterType2 &&
  104. it.GetParameters()[2].ParameterType == parameterType3 &&
  105. it.GetParameters()[3].ParameterType == parameterType4&&
  106. it.GetParameters()[4].ParameterType == parameterType5);
  107. }
  108. public static List<T> ToList<T>(this T thisValue,Func<T,T> action) where T:class,new()
  109. {
  110. return new List<T> { thisValue };
  111. }
  112. public static List<T> ToList<T>(this IEnumerable<T> thisValue, Func<T, T> action) where T : class, new()
  113. {
  114. return thisValue.ToList();
  115. }
  116. public static IEnumerable<T> WhereIF<T>(this IEnumerable<T> thisValue, bool isOk, Func<T, bool> predicate)
  117. {
  118. if (isOk)
  119. {
  120. return thisValue.Where(predicate);
  121. }
  122. else
  123. {
  124. return thisValue;
  125. }
  126. }
  127. public static IEnumerable<T> MappingField<T>(this IEnumerable<T> thisValue,Func<T, object> leftField, Func<object> rightField)
  128. {
  129. return thisValue;
  130. }
  131. public static List<T> MappingField<T>(this T thisValue, Func<T, object> leftField, Func<object> rightField) where T:class
  132. {
  133. return new List<T>() { thisValue };
  134. }
  135. public static bool Any<T>(this IEnumerable<T> thisValue, List<IConditionalModel> conditionalModels)
  136. {
  137. throw new Exception("Can only be used in expressions");
  138. }
  139. public static IEnumerable<T> Where<T>(this IEnumerable<T> thisValue, List<IConditionalModel> conditionalModels)
  140. {
  141. throw new Exception("Can only be used in expressions");
  142. }
  143. }
  144. }
  145. namespace System.Collections.Generic
  146. {
  147. public static class EnumerableExtensions
  148. {
  149. public static bool Contains<T>(this IEnumerable<T> thisValue, T likeKey, bool isNvarchar)
  150. {
  151. return thisValue.Contains(likeKey);
  152. }
  153. }
  154. }