CommonAtrr.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace IMCS.CCS.Utils
  8. {
  9. public class Notify : System.ComponentModel.INotifyPropertyChanged
  10. {
  11. public event PropertyChangedEventHandler PropertyChanged;
  12. public void NotifyProper(string name)
  13. {
  14. if (PropertyChanged != null)
  15. {
  16. PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
  17. }
  18. }
  19. }
  20. public static class CommonAtrr
  21. {
  22. public static short ObjectToShort(this object ob)
  23. {
  24. return short.Parse(ob.ToString());
  25. }
  26. public static byte ObjectToByte(this object ob)
  27. {
  28. return byte.Parse(ob.ToString());
  29. }
  30. /// <summary>
  31. /// 判断当前的FirstOrDefaul是否为空,如果为空,那么不执行参数函数
  32. /// </summary>
  33. /// <typeparam name="TSource"></typeparam>
  34. /// <param name="source"></param>
  35. /// <param name="action"></param>
  36. public static bool FirstOrDefaultYG<TSource>(this TSource source, Action<TSource> action)
  37. {
  38. if (source != null)
  39. {
  40. try
  41. {
  42. action(source);
  43. return true;
  44. }
  45. catch
  46. {
  47. Log.Instance.WriteLogAdd("执行FirstOrDefaultYG函数时出现异常情况");
  48. return false;
  49. }
  50. }
  51. return false;
  52. }
  53. public static bool Foreach<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
  54. {
  55. if (source != null)
  56. {
  57. try
  58. {
  59. foreach (var c in source)
  60. {
  61. action(c);
  62. }
  63. return true;
  64. }
  65. catch
  66. {
  67. Log.Instance.WriteLogAdd("执行Foreach函数时出现异常情况");
  68. return false;
  69. }
  70. }
  71. return false;
  72. }
  73. /// <summary>
  74. /// 把对象转换为字符串
  75. /// </summary>
  76. /// <param name="ob"></param>
  77. /// <returns></returns>
  78. public static string Json_SerializeObject(this object ob)
  79. {
  80. return Newtonsoft.Json.JsonConvert.SerializeObject(ob);
  81. }
  82. /// <summary>
  83. /// 把字符串转换为对象
  84. /// </summary>
  85. /// <typeparam name="T"></typeparam>
  86. /// <param name="value"></param>
  87. /// <returns></returns>
  88. public static T Json_DeserializeObject<T>(this string value)
  89. {
  90. try
  91. {
  92. return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(value);
  93. }
  94. catch
  95. {
  96. Log.Instance.WriteLogAdd(string.Format("转换类型时出现异常{0}-->{1}", typeof(T), value));
  97. return default(T);
  98. }
  99. }
  100. /// <summary>
  101. /// 如果字符串为空, 那么返回true
  102. /// </summary>
  103. /// <param name="msg"></param>
  104. /// <returns></returns>
  105. public static bool StringIsEmpty(this string msg)
  106. {
  107. if (string.IsNullOrEmpty(msg))
  108. {
  109. return true;
  110. }
  111. return false;
  112. }
  113. /// <summary>
  114. /// 通用summary
  115. /// </summary>
  116. /// <typeparam name="T"></typeparam>
  117. /// <param name="t"></param>
  118. /// <param name="sw"></param>
  119. /// <param name="ishead"></param>
  120. /// <returns></returns>
  121. private static bool ObjectWriteCVS<T>(this T t, StreamWriter sw, bool ishead)
  122. {
  123. try
  124. {
  125. System.Reflection.PropertyInfo[] minfo = t.GetType().GetProperties();
  126. if (minfo.Length > 0)
  127. {
  128. foreach (System.Reflection.PropertyInfo mi in minfo)
  129. {
  130. if (mi.PropertyType.IsGenericType)
  131. {
  132. object ob = mi.GetValue(t, null);
  133. int count = (int)ob.GetType().GetProperty("Count").GetValue(ob);//.GetValue(t);
  134. for (int i = 0; i < count; i++)
  135. {
  136. object item = ob.GetType().GetProperty("Item").GetValue(ob, new object[] { i });
  137. if (item.GetType().GetProperties().Where(m => m.Name.Equals("Item_type") && m.GetValue(item).Equals("Elect")).Count() > 0)
  138. {
  139. break;
  140. }
  141. item.ObjectWriteCVS(sw, ishead);
  142. }
  143. }
  144. else
  145. {
  146. //if (mi.Name.Equals("Item_type") && mi.GetValue(t).Equals("Elect"))
  147. //{
  148. // break;
  149. //}
  150. object[] atrti = mi.GetCustomAttributes(false);
  151. if (atrti != null && atrti.Count() > 0)
  152. {
  153. YGAttribute YGa = mi.GetCustomAttributes(false)[0] as YGAttribute;
  154. if (YGa != null && YGa.IsWrite)
  155. {
  156. if (ishead)
  157. {
  158. sw.Write(mi.Name);
  159. sw.Write("\t");
  160. }
  161. else
  162. {
  163. sw.Write(mi.GetValue(t).ToString().Replace("\r", " ").Replace("\n", " ").Replace("\t", " "));
  164. sw.Write("\t");
  165. }
  166. // sw.Write("\t");
  167. }
  168. }
  169. }
  170. }
  171. }
  172. return true;
  173. }
  174. catch (Exception ex)
  175. {
  176. return false;
  177. }
  178. }
  179. /// <summary>
  180. /// 把对象写入到cvs表格里便
  181. /// </summary>
  182. /// <typeparam name="T"></typeparam>
  183. /// <param name="t"></param>
  184. /// <param name="path"></param>
  185. public static bool ObjectWriteCVS<T>(this T t, string path)
  186. {
  187. try
  188. {
  189. System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
  190. StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
  191. System.Reflection.PropertyInfo[] minfo = t.GetType().GetProperties();
  192. if (minfo.Length > 0)
  193. {
  194. foreach (System.Reflection.PropertyInfo mi in minfo)
  195. {
  196. if (mi.PropertyType.IsGenericType)
  197. {
  198. object ob = mi.GetValue(t, null);
  199. int count = (int)ob.GetType().GetProperty("Count").GetValue(ob);//.GetValue(t);
  200. for (int i = 0; i < count; i++)
  201. {
  202. object item = ob.GetType().GetProperty("Item").GetValue(ob, new object[] { i });
  203. foreach (var pro in item.GetType().GetProperties())
  204. {
  205. object[] atrti = pro.GetCustomAttributes(false);
  206. if (atrti != null && atrti.Count() > 0)
  207. {
  208. YGAttribute YGa = pro.GetCustomAttributes(false)[0] as YGAttribute;
  209. if (YGa != null && YGa.IsWrite)
  210. {
  211. sw.Write(pro.GetValue(item));
  212. sw.Write("\t");
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }
  220. sw.WriteLine("");
  221. sw.Flush();
  222. sw.Close();
  223. return true;
  224. }
  225. catch (Exception ex)
  226. {
  227. return false;
  228. }
  229. }
  230. }
  231. /// <summary>
  232. /// 是否写入CVS
  233. /// </summary>
  234. public class YGAttribute : Attribute
  235. {
  236. /// <summary>
  237. /// 是否写入CVS
  238. /// </summary>
  239. public bool IsWrite { get; set; } = true;
  240. }
  241. }