CommonAtrr.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using YG;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.Serialization;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.ComponentModel;
  11. using System.Net.NetworkInformation;
  12. namespace System.Text
  13. {
  14. public class Notify : System.ComponentModel.INotifyPropertyChanged
  15. {
  16. public event PropertyChangedEventHandler PropertyChanged;
  17. public void NotifyProper(string name)
  18. {
  19. if (PropertyChanged != null)
  20. {
  21. PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
  22. }
  23. }
  24. }
  25. public static class CommonAtrr
  26. {
  27. public static void ShowDialog(this System.Windows.Forms.Form fm, string msg, bool isthread = true)
  28. {
  29. if (isthread)
  30. {
  31. Task.Factory.StartNew(() =>
  32. {
  33. System.Windows.Forms.MessageBox.Show(msg, "信息提示", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Asterisk);
  34. });
  35. }
  36. else
  37. {
  38. System.Windows.Forms.MessageBox.Show(msg, "信息提示", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Asterisk);
  39. }
  40. }
  41. /// <summary>
  42. /// 判断text里边是否为空,如果为空那么弹出提示信息
  43. /// </summary>
  44. /// <param name="text"></param>
  45. /// <param name="showmsg"></param>
  46. public static bool Text_Check_value_Empty(this System.Windows.Forms.TextBox text, string showmsg)
  47. {
  48. if (string.IsNullOrEmpty(text.Text))
  49. {
  50. Task.Factory.StartNew(() => { System.Windows.Forms.MessageBox.Show(showmsg, "信息提示", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Asterisk); });
  51. return false;
  52. }
  53. return true;
  54. }
  55. public static byte[] ReturnBtyesWtitString(this string value, int byteleng = 30)
  56. {
  57. byte[] bt = new byte[byteleng];
  58. byte[] item = Encoding.ASCII.GetBytes(value.ToClone());
  59. Array.Copy(item, bt, item.Length);
  60. return bt;
  61. }
  62. public static bool Ping(this string ip)
  63. {
  64. Ping ping = new Ping();
  65. PingReply pingReply = ping.Send(ip.ToClone());
  66. if (pingReply.Status.Equals(IPStatus.Success))
  67. {
  68. return true;
  69. }
  70. return false;
  71. }
  72. /// <summary>
  73. /// 判断当前路径是否存在, 如果不存在就创建,
  74. /// </summary>
  75. /// <param name="path"></param>
  76. /// <returns>如果为false则表示当前路径不存在,true则表示当前路径存在</returns>
  77. public static bool PathIsExist(this string path)
  78. {
  79. try
  80. {
  81. if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(path.ToClone())))
  82. {
  83. System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
  84. }
  85. if (!File.Exists(path))
  86. {
  87. File.Create(path).Close();
  88. return false;
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. YG.Log.Instance.WriteLogAdd($"87-->{ex.Message}");
  94. return false;
  95. }
  96. return true;
  97. }
  98. /// <summary>
  99. /// 判断text里边是否为空,并且文本的长度是否小于当前参数,如果小于那么弹出提示信息
  100. /// </summary>
  101. /// <param name="text"></param>
  102. /// <param name="textleng"></param>
  103. /// <param name="showmsg"></param>
  104. public static bool Text_Check_valueLeng_Empty(this System.Windows.Forms.TextBox text, int textleng, string showmsg)
  105. {
  106. if (string.IsNullOrEmpty(text.Text) && text.Text.Length < textleng)
  107. {
  108. Task.Factory.StartNew(() => { System.Windows.Forms.MessageBox.Show(showmsg); });
  109. return false;
  110. }
  111. return true;
  112. }
  113. /// <summary>
  114. /// 把字符转换为int
  115. /// </summary>
  116. /// <param name="value"></param>
  117. /// <returns></returns>
  118. public static int StringToInt(this string value)
  119. {
  120. try { return Convert.ToInt32(value.ToClone()); }
  121. catch
  122. {
  123. Log.Instance.WriteLogAdd(value + "在进行类型转换StringToInt时出现异常", null);
  124. return 0;
  125. }
  126. }
  127. /// <summary>
  128. /// 把字符转换为int
  129. /// </summary>
  130. /// <param name="value"></param>
  131. /// <returns></returns>
  132. public static bool StringToBool(this string value)
  133. {
  134. try { return Convert.ToBoolean(value.StringToInt()); }
  135. catch
  136. {
  137. Log.Instance.WriteLogAdd(value + "在进行类型转换StringToBool时出现异常", null);
  138. return false;
  139. }
  140. }
  141. /// <summary>
  142. /// 拷贝
  143. /// </summary>
  144. /// <typeparam name="T"></typeparam>
  145. /// <param name="List">The list.</param>
  146. /// <returns>List{``0}.</returns>
  147. public static System.ComponentModel.BindingList<T> Clone<T>(this System.ComponentModel.BindingList<T> List)
  148. {
  149. using (Stream objectStream = new MemoryStream())
  150. {
  151. IFormatter formatter = new BinaryFormatter();
  152. formatter.Serialize(objectStream, List);
  153. objectStream.Seek(0, SeekOrigin.Begin);
  154. return formatter.Deserialize(objectStream) as System.ComponentModel.BindingList<T>;
  155. }
  156. }
  157. public static List<T> Clone<T>(this List<T> List)
  158. {
  159. using (Stream objectStream = new MemoryStream())
  160. {
  161. IFormatter formatter = new BinaryFormatter();
  162. formatter.Serialize(objectStream, List);
  163. objectStream.Seek(0, SeekOrigin.Begin);
  164. return formatter.Deserialize(objectStream) as List<T>;
  165. }
  166. }
  167. public static float StringToFloat(this string value)
  168. {
  169. try { return Convert.ToSingle(value.ToClone()); }
  170. catch
  171. {
  172. Log.Instance.WriteLogAdd(value + "在进行类型转换StringToFloat时出现异常", null);
  173. return 0;
  174. }
  175. }
  176. /// <summary>
  177. /// 把字符转换为double类型
  178. /// </summary>
  179. /// <param name="value"></param>
  180. /// <returns></returns>
  181. public static double StringToDouble(this string value)
  182. {
  183. try { return Convert.ToDouble(value.ToClone()); }
  184. catch
  185. {
  186. Log.Instance.WriteLogAdd(value + "在进行类型转换StringToDouble时出现异常");
  187. return 0;
  188. }
  189. }
  190. /// <summary>
  191. /// 把噢bject类型转换为int类型
  192. /// </summary>
  193. /// <param name="ob"></param>
  194. /// <returns></returns>
  195. public static int ObjectToInt(this object ob)
  196. {
  197. return ob.ToString().StringToInt();
  198. }
  199. public static short ObjectToShort(this object ob)
  200. {
  201. return short.Parse(ob.ToString());
  202. }
  203. public static byte ObjectToByte(this object ob)
  204. {
  205. return byte.Parse(ob.ToString());
  206. }
  207. /// <summary>
  208. /// 把噢bject类型转换为double类型
  209. /// </summary>
  210. /// <param name="ob"></param>
  211. /// <returns></returns>
  212. public static double ObjectToDouble(this object ob)
  213. {
  214. return ob.ToString().StringToDouble();
  215. }
  216. public static string ObjectToString(this object ob)
  217. {
  218. try { if (ob != null) return ob.ToString(); else Log.Instance.WriteLogAdd("转换object为string类型时出现哦bject为空的情况默认返回空字符串"); return ""; }
  219. catch(Exception ex) { YG.Log.Instance.WriteLogAdd($"216-->{ex.Message}"); return ""; }
  220. }
  221. public static void DataGridViewColor(this System.Windows.Forms.DataGridView dv)
  222. {
  223. dv.RowsDefaultCellStyle.BackColor = Drawing.Color.AliceBlue;
  224. dv.AlternatingRowsDefaultCellStyle.BackColor = Drawing.Color.Aquamarine;
  225. }
  226. /// <summary>
  227. /// 判断当前的FirstOrDefaul是否为空,如果为空,那么不执行参数函数
  228. /// </summary>
  229. /// <typeparam name="TSource"></typeparam>
  230. /// <param name="source"></param>
  231. /// <param name="action"></param>
  232. public static bool FirstOrDefaultYG<TSource>(this TSource source, Action<TSource> action)
  233. {
  234. if (source != null)
  235. {
  236. try
  237. {
  238. action(source);
  239. return true;
  240. }
  241. catch
  242. {
  243. Log.Instance.WriteLogAdd("执行FirstOrDefaultYG函数时出现异常情况");
  244. return false;
  245. }
  246. }
  247. return false;
  248. }
  249. public static bool Foreach<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
  250. {
  251. if (source != null)
  252. {
  253. try
  254. {
  255. foreach (var c in source)
  256. {
  257. action(c);
  258. }
  259. return true;
  260. }
  261. catch
  262. {
  263. Log.Instance.WriteLogAdd("执行Foreach函数时出现异常情况");
  264. return false;
  265. }
  266. }
  267. return false;
  268. }
  269. /// <summary>
  270. /// 把对象转换为字符串
  271. /// </summary>
  272. /// <param name="ob"></param>
  273. /// <returns></returns>
  274. public static string Json_SerializeObject(this object ob)
  275. {
  276. return Newtonsoft.Json.JsonConvert.SerializeObject(ob);
  277. }
  278. /// <summary>
  279. /// 把字符串转换为对象
  280. /// </summary>
  281. /// <typeparam name="T"></typeparam>
  282. /// <param name="value"></param>
  283. /// <returns></returns>
  284. public static T Json_DeserializeObject<T>(this string value)
  285. {
  286. try
  287. {
  288. return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(value);
  289. }
  290. catch
  291. {
  292. Log.Instance.WriteLogAdd(string.Format("转换类型时出现异常{0}-->{1}", typeof(T), value));
  293. return default(T);
  294. }
  295. }
  296. /// <summary>
  297. /// 如果字符串为空, 那么返回true
  298. /// </summary>
  299. /// <param name="msg"></param>
  300. /// <returns></returns>
  301. public static bool StringIsEmpty(this string msg)
  302. {
  303. if (string.IsNullOrEmpty(msg))
  304. {
  305. return true;
  306. }
  307. return false;
  308. }
  309. /// <summary>
  310. /// 通用summary
  311. /// </summary>
  312. /// <typeparam name="T"></typeparam>
  313. /// <param name="t"></param>
  314. /// <param name="sw"></param>
  315. /// <param name="ishead"></param>
  316. /// <returns></returns>
  317. private static bool ObjectWriteCVS<T>(this T t, StreamWriter sw, bool ishead)
  318. {
  319. try
  320. {
  321. System.Reflection.PropertyInfo[] minfo = t.GetType().GetProperties();
  322. if (minfo.Length > 0)
  323. {
  324. foreach (System.Reflection.PropertyInfo mi in minfo)
  325. {
  326. if (mi.PropertyType.IsGenericType)
  327. {
  328. object ob = mi.GetValue(t, null);
  329. int count = (int)ob.GetType().GetProperty("Count").GetValue(ob);//.GetValue(t);
  330. for (int i = 0; i < count; i++)
  331. {
  332. object item = ob.GetType().GetProperty("Item").GetValue(ob, new object[] { i });
  333. if (item.GetType().GetProperties().Where(m => m.Name.Equals("Item_type") && m.GetValue(item).Equals("Elect")).Count() > 0)
  334. {
  335. break;
  336. }
  337. item.ObjectWriteCVS(sw, ishead);
  338. }
  339. }
  340. else
  341. {
  342. //if (mi.Name.Equals("Item_type") && mi.GetValue(t).Equals("Elect"))
  343. //{
  344. // break;
  345. //}
  346. object[] atrti = mi.GetCustomAttributes(false);
  347. if (atrti != null && atrti.Count() > 0)
  348. {
  349. YGAttribute YGa = mi.GetCustomAttributes(false)[0] as YGAttribute;
  350. if (YGa != null && YGa.IsWrite)
  351. {
  352. if (ishead)
  353. {
  354. sw.Write(mi.Name);
  355. sw.Write("\t");
  356. }
  357. else
  358. {
  359. sw.Write(mi.GetValue(t).ToString().Replace("\r", " ").Replace("\n", " ").Replace("\t", " "));
  360. sw.Write("\t");
  361. }
  362. // sw.Write("\t");
  363. }
  364. }
  365. }
  366. }
  367. }
  368. return true;
  369. }
  370. catch (Exception ex)
  371. {
  372. YG.Log.Instance.WriteLogAdd($"377-->{ex.Message}");
  373. return false;
  374. }
  375. }
  376. /// <summary>
  377. /// 把对象写入到cvs表格里便
  378. /// </summary>
  379. /// <typeparam name="T"></typeparam>
  380. /// <param name="t"></param>
  381. /// <param name="path"></param>
  382. public static bool ObjectWriteCVS<T>(this T t, string path)
  383. {
  384. try
  385. {
  386. System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
  387. StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
  388. System.Reflection.PropertyInfo[] minfo = t.GetType().GetProperties();
  389. if (minfo.Length > 0)
  390. {
  391. foreach (System.Reflection.PropertyInfo mi in minfo)
  392. {
  393. if (mi.PropertyType.IsGenericType)
  394. {
  395. object ob = mi.GetValue(t, null);
  396. int count = (int)ob.GetType().GetProperty("Count").GetValue(ob);//.GetValue(t);
  397. for (int i = 0; i < count; i++)
  398. {
  399. object item = ob.GetType().GetProperty("Item").GetValue(ob, new object[] { i });
  400. foreach (var pro in item.GetType().GetProperties())
  401. {
  402. object[] atrti = pro.GetCustomAttributes(false);
  403. if (atrti != null && atrti.Count() > 0)
  404. {
  405. YGAttribute YGa = pro.GetCustomAttributes(false)[0] as YGAttribute;
  406. if (YGa != null && YGa.IsWrite)
  407. {
  408. sw.Write(pro.GetValue(item));
  409. sw.Write("\t");
  410. }
  411. }
  412. }
  413. }
  414. }
  415. }
  416. }
  417. sw.WriteLine("");
  418. sw.Flush();
  419. sw.Close();
  420. return true;
  421. }
  422. catch (Exception ex)
  423. {
  424. YG.Log.Instance.WriteLogAdd(ex.Message);
  425. return false;
  426. }
  427. }
  428. }
  429. /// <summary>
  430. /// 是否写入CVS
  431. /// </summary>
  432. public class YGAttribute : Attribute
  433. {
  434. /// <summary>
  435. /// 是否写入CVS
  436. /// </summary>
  437. public bool IsWrite { get; set; } = true;
  438. }
  439. }