ExtensionMethod.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace WebAPIBase.Utils
  8. {
  9. public static partial class ExtensionMethod
  10. {
  11. #region string判断扩展
  12. public static bool IsNullOrEmpty(this string str)
  13. {
  14. return StringHelper.IsNullOrEmpty(str);
  15. }
  16. public static bool IsNotNullAndEmpty(this string str)
  17. {
  18. return StringHelper.IsNotNullAndEmpty(str);
  19. }
  20. /// <summary>
  21. /// 判断是否是整数
  22. /// </summary>
  23. /// <param name="str"></param>
  24. /// <returns></returns>
  25. public static bool IsInteger(this string str)
  26. {
  27. return StringHelper.IsInteger(str);
  28. }
  29. /// <summary>
  30. /// 判断字符串是否是正整数,可以是无数位
  31. /// </summary>
  32. /// <param name="str"></param>
  33. /// <returns></returns>
  34. public static bool IsPositiveInteger(this string str)
  35. {
  36. return StringHelper.IsPositiveInteger(str);
  37. }
  38. /// <summary>
  39. /// 判断是否为decimal类型
  40. /// </summary>
  41. /// <param name="str"></param>
  42. /// <returns></returns>
  43. public static bool IsDecimal(this string str)
  44. {
  45. return StringHelper.IsDecimal(str);
  46. }
  47. public static bool IsNumber(this string str)
  48. {
  49. return StringHelper.IsNumber(str);
  50. }
  51. /// <summary>
  52. /// 判断是否为手机号
  53. /// </summary>
  54. /// <param name="str"></param>
  55. /// <returns></returns>
  56. public static bool IsCellPhone(this string str)
  57. {
  58. return StringHelper.IsCellPhone(str);
  59. }
  60. /// <summary>
  61. /// 是否固定电话
  62. /// </summary>
  63. /// <param name="str"></param>
  64. /// <returns></returns>
  65. public static bool IsPhone(this string str)
  66. {
  67. return StringHelper.IsPhone(str);
  68. }
  69. /// <summary>
  70. /// 判断是否为邮箱地址
  71. /// </summary>
  72. /// <param name="str"></param>
  73. /// <returns></returns>
  74. public static bool IsEmail(this string str)
  75. {
  76. return StringHelper.IsEmail(str);
  77. }
  78. /// <summary>
  79. /// 判断身份证号格式是否正确
  80. /// </summary>
  81. /// <param name="str"></param>
  82. /// <returns></returns>
  83. public static bool IsIDCard(this string str)
  84. {
  85. return StringHelper.IsIDCard(str);
  86. }
  87. #endregion
  88. #region object 判断扩展
  89. /// <summary>
  90. /// 是null或空
  91. /// </summary>
  92. /// <param name="obj"></param>
  93. /// <returns></returns>
  94. public static bool IsNullOrEmpty(this object obj)
  95. {
  96. return obj == null ? true : StringHelper.IsNullOrEmpty(obj.ToString());
  97. }
  98. public static bool IsNotNullAndEmpty(this object obj)
  99. {
  100. return obj == null ? false : StringHelper.IsNotNullAndEmpty(obj.ToString());
  101. }
  102. public static bool IsInteger(this object obj)
  103. {
  104. return obj == null ? false : StringHelper.IsInteger(obj.ToString());
  105. }
  106. public static bool IsDecimal(this object obj)
  107. {
  108. return obj == null ? false : StringHelper.IsDecimal(obj.ToString());
  109. }
  110. public static bool IsNumber(this object obj)
  111. {
  112. return obj == null ? false : StringHelper.IsNumber(obj.ToString());
  113. }
  114. /// <summary>
  115. /// 对象转换为字符串,如果对象为null,则为空
  116. /// </summary>
  117. /// <param name="obj"></param>
  118. /// <returns></returns>
  119. public static string SConvertString(this object obj)
  120. {
  121. return StringHelper.SConvertString(obj);
  122. }
  123. public static DataSet ToDataSet<T>(this IList<T> list)
  124. {
  125. Type elementType = typeof(T);
  126. var ds = new DataSet();
  127. var t = new DataTable();
  128. ds.Tables.Add(t);
  129. elementType.GetProperties().ToList().ForEach(propInfo => t.Columns.Add(propInfo.Name, Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType));
  130. foreach (T item in list)
  131. {
  132. var row = t.NewRow();
  133. elementType.GetProperties().ToList().ForEach(propInfo => row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value);
  134. t.Rows.Add(row);
  135. }
  136. return ds;
  137. }
  138. #endregion
  139. #region 转换扩展
  140. /// <summary>
  141. /// 转换为int32类型,若是非数值型字串,则变为默认值0
  142. /// </summary>
  143. /// <param name="str"></param>
  144. /// <returns></returns>
  145. public static int ToInteger(this object str)
  146. {
  147. try
  148. {
  149. return Convert.ToInt32(str);
  150. }
  151. catch
  152. {
  153. return 0;
  154. }
  155. }
  156. /// <summary>
  157. /// 转换为Double类型,若是非数值型字串,则变为默认值0
  158. /// </summary>
  159. /// <param name="str"></param>
  160. /// <returns></returns>
  161. public static double ToNumber(this object str)
  162. {
  163. try
  164. {
  165. return Convert.ToDouble(str);
  166. }
  167. catch
  168. {
  169. return 0;
  170. }
  171. }
  172. /// <summary>
  173. /// 转换为Decimal类型,若是非数值型字串,则变为默认值0
  174. /// </summary>
  175. /// <param name="str"></param>
  176. /// <returns></returns>
  177. public static decimal ToDecimal(this object str)
  178. {
  179. try
  180. {
  181. return Convert.ToDecimal(str);
  182. }
  183. catch
  184. {
  185. return 0;
  186. }
  187. }
  188. public static DateTime ToDateTime(this string str)
  189. {
  190. return DateTime.Parse(str);
  191. }
  192. public static string FormatWith(this string str, params object[] objs)
  193. {
  194. return string.Format(str, objs);
  195. }
  196. /// <summary>
  197. /// 首字母小写
  198. /// </summary>
  199. /// <param name="str"></param>
  200. /// <returns></returns>
  201. ///
  202. public static string ToCamel(this string str)
  203. {
  204. if (str.IsNullOrEmpty())
  205. {
  206. return str;
  207. }
  208. else
  209. {
  210. if (str.Length > 1)
  211. {
  212. return str[0].ToString().ToLower() + str.Substring(1);
  213. }
  214. else
  215. {
  216. return str.ToLower();
  217. }
  218. }
  219. }
  220. /// <summary>
  221. ///首字母大写
  222. /// </summary>
  223. /// <param name="str"></param>
  224. /// <returns></returns>
  225. public static string ToPascal(this string str)
  226. {
  227. if (str.IsNullOrEmpty())
  228. {
  229. return str;
  230. }
  231. else
  232. {
  233. if (str.Length > 1)
  234. {
  235. return str[0].ToString().ToUpper() + str.Substring(1);
  236. }
  237. else
  238. {
  239. return str.ToLower();
  240. }
  241. }
  242. }
  243. /// <summary>
  244. /// 得到大写
  245. /// </summary>
  246. /// <param name="str"></param>
  247. /// <returns></returns>
  248. public static string To32MD5(this string str)
  249. {
  250. return StringHelper.EncryptMD5(str);
  251. }
  252. /// <summary>
  253. /// 得到小写
  254. /// </summary>
  255. /// <param name="str"></param>
  256. /// <returns></returns>
  257. public static string To16MD5(this string str)
  258. {
  259. return StringHelper.ASP16MD5(str);
  260. }
  261. #endregion
  262. #region 拼音
  263. public static string ToChineseSpell(this string str)
  264. {
  265. return StringHelper.GetChineseSpell(str);
  266. }
  267. public static string ToChineseIndex(this string str)
  268. {
  269. return StringHelper.GetChineseIndex(str);
  270. }
  271. #endregion
  272. }
  273. }