ExtensionMethod.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 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. /// <summary>
  88. /// 是否是日期判断
  89. /// </summary>
  90. /// <param name="str"></param>
  91. /// <returns></returns>
  92. public static bool IsDate(this string str)
  93. {
  94. return StringHelper.IsDate(str);
  95. }
  96. #endregion
  97. #region object 判断扩展
  98. /// <summary>
  99. /// 是null或空
  100. /// </summary>
  101. /// <param name="obj"></param>
  102. /// <returns></returns>
  103. public static bool IsNullOrEmpty(this object obj)
  104. {
  105. return obj == null ? true : StringHelper.IsNullOrEmpty(obj.ToString());
  106. }
  107. public static bool IsNotNullAndEmpty(this object obj)
  108. {
  109. return obj == null ? false : StringHelper.IsNotNullAndEmpty(obj.ToString());
  110. }
  111. public static bool IsInteger(this object obj)
  112. {
  113. return obj == null ? false : StringHelper.IsInteger(obj.ToString());
  114. }
  115. public static bool IsDecimal(this object obj)
  116. {
  117. return obj == null ? false : StringHelper.IsDecimal(obj.ToString());
  118. }
  119. public static bool IsNumber(this object obj)
  120. {
  121. return obj == null ? false : StringHelper.IsNumber(obj.ToString());
  122. }
  123. /// <summary>
  124. /// 是否是日期判断
  125. /// </summary>
  126. /// <param name="str"></param>
  127. /// <returns></returns>
  128. public static bool IsDate(this object obj)
  129. {
  130. return obj == null ? false : StringHelper.IsDate(obj.ToString());
  131. }
  132. /// <summary>
  133. /// 对象转换为字符串,如果对象为null,则为空
  134. /// </summary>
  135. /// <param name="obj"></param>
  136. /// <returns></returns>
  137. public static string SConvertString(this object obj)
  138. {
  139. return StringHelper.SConvertString(obj);
  140. }
  141. public static DataSet ToDataSet<T>(this IList<T> list)
  142. {
  143. Type elementType = typeof(T);
  144. var ds = new DataSet();
  145. var t = new DataTable();
  146. ds.Tables.Add(t);
  147. elementType.GetProperties().ToList().ForEach(propInfo => t.Columns.Add(propInfo.Name, Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType));
  148. foreach (T item in list)
  149. {
  150. var row = t.NewRow();
  151. elementType.GetProperties().ToList().ForEach(propInfo => row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value);
  152. t.Rows.Add(row);
  153. }
  154. return ds;
  155. }
  156. #endregion
  157. #region 转换扩展
  158. /// <summary>
  159. /// 转换为int32类型,若是非数值型字串,则变为默认值0
  160. /// </summary>
  161. /// <param name="str"></param>
  162. /// <returns></returns>
  163. public static int ToInteger(this object str)
  164. {
  165. try
  166. {
  167. return Convert.ToInt32(str);
  168. }
  169. catch
  170. {
  171. return 0;
  172. }
  173. }
  174. /// <summary>
  175. /// 转换为Double类型,若是非数值型字串,则变为默认值0
  176. /// </summary>
  177. /// <param name="str"></param>
  178. /// <returns></returns>
  179. public static double ToNumber(this object str)
  180. {
  181. try
  182. {
  183. return Convert.ToDouble(str);
  184. }
  185. catch
  186. {
  187. return 0;
  188. }
  189. }
  190. /// <summary>
  191. /// 转换为Decimal类型,若是非数值型字串,则变为默认值0
  192. /// </summary>
  193. /// <param name="str"></param>
  194. /// <returns></returns>
  195. public static decimal ToDecimal(this object str)
  196. {
  197. try
  198. {
  199. return Convert.ToDecimal(str);
  200. }
  201. catch
  202. {
  203. return 0;
  204. }
  205. }
  206. public static DateTime ToDateTime(this string str)
  207. {
  208. return DateTime.Parse(str);
  209. }
  210. public static string FormatWith(this string str, params object[] objs)
  211. {
  212. return string.Format(str, objs);
  213. }
  214. /// <summary>
  215. /// 首字母小写
  216. /// </summary>
  217. /// <param name="str"></param>
  218. /// <returns></returns>
  219. ///
  220. public static string ToCamel(this string str)
  221. {
  222. if (str.IsNullOrEmpty())
  223. {
  224. return str;
  225. }
  226. else
  227. {
  228. if (str.Length > 1)
  229. {
  230. return str[0].ToString().ToLower() + str.Substring(1);
  231. }
  232. else
  233. {
  234. return str.ToLower();
  235. }
  236. }
  237. }
  238. /// <summary>
  239. ///首字母大写
  240. /// </summary>
  241. /// <param name="str"></param>
  242. /// <returns></returns>
  243. public static string ToPascal(this string str)
  244. {
  245. if (str.IsNullOrEmpty())
  246. {
  247. return str;
  248. }
  249. else
  250. {
  251. if (str.Length > 1)
  252. {
  253. return str[0].ToString().ToUpper() + str.Substring(1);
  254. }
  255. else
  256. {
  257. return str.ToLower();
  258. }
  259. }
  260. }
  261. /// <summary>
  262. /// 得到大写
  263. /// </summary>
  264. /// <param name="str"></param>
  265. /// <returns></returns>
  266. public static string To32MD5(this string str)
  267. {
  268. return StringHelper.EncryptMD5(str);
  269. }
  270. /// <summary>
  271. /// 得到小写
  272. /// </summary>
  273. /// <param name="str"></param>
  274. /// <returns></returns>
  275. public static string To16MD5(this string str)
  276. {
  277. return StringHelper.ASP16MD5(str);
  278. }
  279. #endregion
  280. #region 拼音
  281. public static string ToChineseSpell(this string str)
  282. {
  283. return StringHelper.GetChineseSpell(str);
  284. }
  285. public static string ToChineseIndex(this string str)
  286. {
  287. return StringHelper.GetChineseIndex(str);
  288. }
  289. #endregion
  290. }
  291. }