BaseClass.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace IPADDemo.Util
  10. {
  11. public static class BaseClass
  12. {
  13. /// <summary>
  14. /// 判断是否为空,为空返回true
  15. /// </summary>
  16. /// <param name="data"></param>
  17. /// <returns></returns>
  18. public static bool IsNull(this object data)
  19. {
  20. //如果为null
  21. if (data == null)
  22. {
  23. return true;
  24. }
  25. //如果为""
  26. if (data.GetType() == typeof(String))
  27. {
  28. if (string.IsNullOrEmpty(data.ToString().Trim()))
  29. {
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35. /// <summary>
  36. /// 判断是否为空,为空返回true
  37. /// </summary>
  38. /// <param name="data"></param>
  39. /// <returns></returns>
  40. public static bool IsNull(string data)
  41. {
  42. //如果为null
  43. if (data == null)
  44. {
  45. return true;
  46. }
  47. //如果为""
  48. if (data.GetType() == typeof(String))
  49. {
  50. if (string.IsNullOrEmpty(data.ToString().Trim()))
  51. {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. /// <summary>
  58. /// 将obj类型转换为string
  59. /// </summary>
  60. /// <param name="s"></param>
  61. /// <returns></returns>
  62. public static string ConvertToString(this object s)
  63. {
  64. if (s == null)
  65. {
  66. return "";
  67. }
  68. else
  69. {
  70. return Convert.ToString(s);
  71. }
  72. }
  73. /// <summary>
  74. /// 将字符串转int32
  75. /// </summary>
  76. /// <param name="s"></param>
  77. /// <returns></returns>
  78. public static Int32 ConvertToInt32(this string s)
  79. {
  80. int i = 0;
  81. if (s == null)
  82. {
  83. return 0;
  84. }
  85. else
  86. {
  87. int.TryParse(s, out i);
  88. }
  89. return i;
  90. }
  91. /// <summary>
  92. /// 将字符串转double
  93. /// </summary>
  94. /// <param name="s"></param>
  95. /// <returns></returns>
  96. public static double ConvertToDouble(this object s)
  97. {
  98. double i = 0;
  99. if (s == null)
  100. {
  101. return 0;
  102. }
  103. else
  104. {
  105. double.TryParse(s.ToString(), out i);
  106. }
  107. return i;
  108. }
  109. /// <summary>
  110. /// 转换为datetime类型
  111. /// </summary>
  112. /// <param name="s"></param>
  113. /// <returns></returns>
  114. public static DateTime ConvertToDateTime(this string s)
  115. {
  116. DateTime dt = new DateTime();
  117. if (s == null || s == "")
  118. {
  119. return DateTime.Now;
  120. }
  121. DateTime.TryParse(s, out dt);
  122. return dt;
  123. }
  124. /// <summary>
  125. /// 转换为datetime类型的格式字符串
  126. /// </summary>
  127. /// <param name="s">要转换的对象</param>
  128. /// <param name="y">格式化字符串</param>
  129. /// <returns></returns>
  130. public static string ConvertToDateTime(this string s, string y)
  131. {
  132. DateTime dt = new DateTime();
  133. DateTime.TryParse(s, out dt);
  134. return dt.ToString(y);
  135. }
  136. /// <summary>
  137. /// 将字符串转换成decimal
  138. /// </summary>
  139. /// <param name="s"></param>
  140. /// <returns></returns>
  141. public static decimal ConvertToDecimal(this object s)
  142. {
  143. decimal d = 0;
  144. if (s == null || s == "")
  145. {
  146. return 0;
  147. }
  148. Decimal.TryParse(s.ToString(), out d);
  149. return d;
  150. }
  151. public static string ReplaceHtml(this string s)
  152. {
  153. return s.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;", "&").Replace("&quot;", "\"");
  154. }
  155. public static string ImageToBase64String(this Image image)
  156. {
  157. return Convert.ToBase64String(image.ImageToBytes());
  158. }
  159. public static Image Base64StringToImage(this string base64)
  160. {
  161. byte[] buffer = Convert.FromBase64String(base64);
  162. return buffer.BytesToImage();
  163. }
  164. public static byte[] ImageToBytes(this Image image)
  165. {
  166. ImageFormat format = image.RawFormat;
  167. using (MemoryStream ms = new MemoryStream())
  168. {
  169. image.Save(ms, ImageFormat.Png);
  170. byte[] buffer = new byte[ms.Length];
  171. //Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
  172. ms.Seek(0, SeekOrigin.Begin);
  173. ms.Read(buffer, 0, buffer.Length);
  174. return buffer;
  175. }
  176. }
  177. public static Image BytesToImage(this byte[] buffer)
  178. {
  179. MemoryStream ms = new MemoryStream(buffer);
  180. Image image = Image.FromStream(ms);
  181. return image;
  182. }
  183. /// <summary>
  184. /// utf8转ansi
  185. /// </summary>
  186. /// <param name="str"></param>
  187. /// <returns></returns>
  188. public static string Utf8ToAnsi(this string str) {
  189. return Cover.MByteToWChar(str, 936);
  190. }
  191. }
  192. }