CommonAtrr.cs 17 KB

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