ExtendClass.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using Infragistics.Win.UltraWinGrid;
  7. using Microsoft.CSharp;
  8. using System.CodeDom.Compiler;
  9. using System.Windows.Forms;
  10. using System.Reflection;
  11. using Core.Mes.Client.Comm.Tool;
  12. namespace System
  13. {
  14. /// <summary>
  15. /// 附加类
  16. /// </summary>
  17. public static class ExtendClass
  18. {
  19. /// <summary>
  20. /// 批量添加参数
  21. /// </summary>
  22. /// <param name="list"></param>
  23. /// <param name="args">参数</param>
  24. public static void AddRange(this ArrayList list, params string[] args)
  25. {
  26. foreach (string str in args)
  27. {
  28. list.Add(str);
  29. }
  30. }
  31. /// <summary>
  32. /// 批量添加参数
  33. /// </summary>
  34. /// <param name="list"></param>
  35. /// <param name="args">参数</param>
  36. public static void AddRange(this List<string> list, params string[] args)
  37. {
  38. foreach (string str in args)
  39. {
  40. list.Add(str);
  41. }
  42. }
  43. /// <summary>
  44. /// 是否可以转换为整型
  45. /// </summary>
  46. /// <param name="str">字符串</param>
  47. /// <returns>是否可以转换</returns>
  48. public static bool TryParseInt(this string str)
  49. {
  50. int a = 0;
  51. return int.TryParse(str, out a);
  52. }
  53. /// <summary>
  54. /// 是否可以转换为双浮点型
  55. /// </summary>
  56. /// <param name="str">字符串</param>
  57. /// <returns>是否可以转换</returns>
  58. public static bool TryParseDouble(this string str)
  59. {
  60. double a = 0.0D;
  61. return double.TryParse(str, out a);
  62. }
  63. /// <summary>
  64. /// 是否可以转换为货币型
  65. /// </summary>
  66. /// <param name="str">字符串</param>
  67. /// <returns>是否可以转换</returns>
  68. public static bool TryParseDecimal(this string str)
  69. {
  70. decimal a = 0.0M;
  71. return decimal.TryParse(str, out a);
  72. }
  73. /// <summary>
  74. /// 查询
  75. /// </summary>
  76. /// <param name="qRows">IQueryable对象</param>
  77. /// <param name="filterExpression">条件筛选表达式</param>
  78. /// <returns></returns>
  79. public static IQueryable<UltraGridRow> Where(this IQueryable<UltraGridRow> qRows,
  80. string filterExpression)
  81. {
  82. string[] strings = filterExpression.Trim().Split(' ');
  83. string[] strSings = new string[] { "=", ">", ">=", "<", "<=", "!=", "<>" };
  84. string field = strings[0].Trim();
  85. string sing = strings[1].Trim();
  86. string value = strings[2].Trim(' ', '\'');
  87. if (strSings.Contains(sing) == false) throw new Exception("表达式错误!");
  88. switch (sing)
  89. {
  90. case "=":
  91. qRows = from a in qRows
  92. where a.Cells[field].Value.ToString() == value
  93. select a;
  94. break;
  95. case ">":
  96. qRows = qRows.Where(a =>
  97. a.Cells[field].Value.ToString() != ""
  98. && a.Cells[field].Value.ToString().TryParseDecimal()
  99. && decimal.Parse(a.Cells[field].Value.ToString()) > decimal.Parse(value));
  100. break;
  101. case ">=":
  102. qRows = qRows.Where(a =>
  103. a.Cells[field].Value.ToString() != ""
  104. && a.Cells[field].Value.ToString().TryParseDecimal()
  105. && decimal.Parse(a.Cells[field].Value.ToString()) >= decimal.Parse(value));
  106. break;
  107. case "<":
  108. qRows = qRows.Where(a =>
  109. a.Cells[field].Value.ToString() != ""
  110. && a.Cells[field].Value.ToString().TryParseDecimal()
  111. && decimal.Parse(a.Cells[field].Value.ToString()) < decimal.Parse(value));
  112. break;
  113. case "<=":
  114. qRows = qRows.Where(a =>
  115. a.Cells[field].Value.ToString() != ""
  116. && a.Cells[field].Value.ToString().TryParseDecimal()
  117. && decimal.Parse(a.Cells[field].Value.ToString()) <= decimal.Parse(value));
  118. break;
  119. case "!=":
  120. case "<>":
  121. qRows = from a in qRows
  122. where a.Cells[field].Value.ToString() != value
  123. select a;
  124. break;
  125. }
  126. return qRows;
  127. }
  128. private static CSharpCodeProvider provider;
  129. private static CompilerParameters parameter;
  130. ///// <summary>
  131. ///// 将输入的计算公式 动态编译,再计算其值
  132. ///// </summary>
  133. ///// <param name="formula">输入的表现公式</param>
  134. ///// <returns></returns>
  135. //public static decimal? CompileFormula(this string formula)
  136. //{
  137. // //1942.57*(Axc^0.2)/(517^0.9) 8^2^4
  138. // formula = ReplacePowSign(formula, 0);
  139. // if(provider == null)
  140. // {
  141. // provider = new CSharpCodeProvider();
  142. // parameter = new CompilerParameters();
  143. // parameter.ReferencedAssemblies.Add("System.dll");
  144. // parameter.GenerateExecutable = false;
  145. // parameter.GenerateInMemory = true;
  146. // }
  147. // CompilerResults result = provider.CompileAssemblyFromSource(parameter,
  148. // CreateCode(formula));//将你的式子放在这里
  149. // if (result.Errors.Count > 0)
  150. // {
  151. // return null;
  152. // }
  153. // else
  154. // {
  155. // Assembly assembly = result.CompiledAssembly;
  156. // Type AType = assembly.GetType("ANameSpace.AClass");
  157. // MethodInfo method = AType.GetMethod("AFunc");
  158. // decimal count = decimal.Parse(method.Invoke(null, null).ToString());
  159. // return count;
  160. // }
  161. //}
  162. private static Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
  163. /// <summary>
  164. /// 将输入的计算公式,通过脚本计算
  165. /// </summary>
  166. /// <param name="formula">输入的表现公式</param>
  167. /// <returns></returns>
  168. public static decimal? CompileFormula(this string formula)
  169. {
  170. //1942.57*(Axc^0.2)/(517^0.9) 8^2^4
  171. formula = powSign(formula);
  172. //1942.57*(Math.Pow((double)1, (double)0.2))/(Math.Pow((double)517, (double)0.9))
  173. decimal? value = null;
  174. try
  175. {
  176. value = decimal.Parse(Microsoft.JScript.Eval.JScriptEvaluate(formula, ve).ToString());
  177. }
  178. catch (Exception ex)
  179. {
  180. return null;
  181. }
  182. return value;
  183. }
  184. /// <summary>
  185. /// 计算字符串表达式 如:(2*a-8)/d 或者三元运算 (2*a-8)/d > 0 ? 2*a : (2*a + 1)
  186. /// </summary>
  187. /// <param name="formula">输入的表现公式</param>
  188. /// <returns></returns>
  189. public static string Eval(this string express)
  190. {
  191. //1942.57*(Axc^0.2)/(517^0.9) 8^2^4
  192. express = powSign(express);
  193. //1942.57*(Math.Pow((double)1, (double)0.2))/(Math.Pow((double)517, (double)0.9))
  194. string result = "";
  195. try
  196. {
  197. result = Microsoft.JScript.Eval.JScriptEvaluate(express, ve).ToString();
  198. }
  199. catch (Exception ex)
  200. {
  201. return null;
  202. }
  203. return result;
  204. }
  205. private static string powSign(string express)
  206. {
  207. if (!express.Contains("^")) return express;
  208. //2+(6+2)^2+2^2
  209. String sings = "+-*/()^";
  210. int index = express.IndexOf("^");
  211. char[] strChars = express.Substring(0, index).ToCharArray();
  212. char[] str2Chars = express.Substring(index + 1).ToCharArray();
  213. StringBuilder strBld = new StringBuilder();
  214. strBld.Append(strChars);
  215. int khCnt = 0;
  216. bool isKh = false;
  217. for (int i = strChars.Length - 1; i >= 0; i--)
  218. {
  219. char strChar = strChars[i];
  220. if (strChar == ')')
  221. {
  222. if (i == strChars.Length - 1)
  223. {
  224. isKh = true;
  225. }
  226. khCnt++;
  227. }
  228. else if (strChar == '(')
  229. {
  230. khCnt--;
  231. }
  232. if (isKh)
  233. {
  234. if (khCnt == 0)
  235. {
  236. strBld.Insert(i, "Math.pow(");
  237. break;
  238. }
  239. }
  240. else
  241. {
  242. if (i == 0)
  243. {
  244. strBld.Insert(i, "Math.pow(");
  245. break;
  246. }
  247. else if (sings.IndexOf(strChar) > -1)
  248. {
  249. strBld.Insert(i + 1, "Math.pow(");
  250. break;
  251. }
  252. }
  253. }
  254. //2+(6+2)^2+2^2
  255. StringBuilder str2Bld = new StringBuilder();
  256. str2Bld.Append(str2Chars);
  257. khCnt = 0;
  258. isKh = false;
  259. for (int i = 0; i < str2Chars.Length; i++)
  260. {
  261. char strChar = str2Chars[i];
  262. if (i == 0)
  263. {
  264. str2Bld.Insert(0, ", ");
  265. if (strChar == '(')
  266. {
  267. isKh = true;
  268. }
  269. }
  270. if (strChar == '(')
  271. {
  272. khCnt++;
  273. }
  274. else if (strChar == ')')
  275. {
  276. khCnt--;
  277. }
  278. if (isKh)
  279. {
  280. if (khCnt == 0)
  281. {
  282. str2Bld.Insert(i + 3, ")");
  283. break;
  284. }
  285. }
  286. else
  287. {
  288. if (i == str2Chars.Length - 1)
  289. {
  290. str2Bld.Insert(i + 3, ")");
  291. break;
  292. }
  293. else if (sings.IndexOf(strChar) > -1)
  294. {
  295. str2Bld.Insert(i + 2, ")");
  296. break;
  297. }
  298. }
  299. }
  300. express = strBld.ToString() + str2Bld.ToString();
  301. return powSign(express);
  302. }
  303. /// <summary>
  304. /// 获取Cell实际值
  305. /// </summary>
  306. /// <param name="row">UltraGridRow</param>
  307. /// <param name="columnName">列名</param>
  308. /// <returns>实际值</returns>
  309. public static string GetValue(this UltraGridRow row, string columnName)
  310. {
  311. try
  312. {
  313. return row.Cells[columnName].Value.ToString2();
  314. }
  315. catch (Exception ex)
  316. {
  317. throw;
  318. }
  319. }
  320. /// <summary>
  321. /// 设置Cell实际值
  322. /// </summary>
  323. /// <param name="row">UltraGridRow</param>
  324. /// <param name="columnName">列名</param>
  325. /// <param name="value">值</param>
  326. public static void SetValue(this UltraGridRow row, string columnName, string value)
  327. {
  328. row.Cells[columnName].Value = value;
  329. }
  330. /// <summary>
  331. /// 获取Cell显示值
  332. /// </summary>
  333. /// <param name="row">UltraGridRow</param>
  334. /// <param name="columnName">列名</param>
  335. /// <returns>显示值</returns>
  336. public static string GetText(this UltraGridRow row, string columnName)
  337. {
  338. return row.Cells[columnName].Text.Trim();
  339. }
  340. /// <summary>
  341. /// 获取激活行的Cell实际值
  342. /// </summary>
  343. /// <param name="grid">grid</param>
  344. /// <param name="columnName">列名</param>
  345. /// <returns>实际值</returns>
  346. public static string GetActiveRowValue(this UltraGrid grid, string columnName)
  347. {
  348. return grid.ActiveRow.Cells[columnName].Value.ToString();
  349. }
  350. /// <summary>
  351. /// 设置激活行的Cell实际值
  352. /// </summary>
  353. /// <param name="grid">UltraGrid</param>
  354. /// <param name="columnName">列名</param>
  355. /// <param name="value">值</param>
  356. public static void SetActiveRowValue(this UltraGrid grid, string columnName, string value)
  357. {
  358. grid.ActiveRow.Cells[columnName].Value = value;
  359. }
  360. /// <summary>
  361. /// 获取激活行的Cell显示值
  362. /// </summary>
  363. /// <param name="grid">grid</param>
  364. /// <param name="columnName">列名</param>
  365. /// <returns>显示值</returns>
  366. public static string GetActiveRowText(this UltraGrid grid, string columnName)
  367. {
  368. return grid.ActiveRow.Cells[columnName].Text.ToString();
  369. }
  370. /// <summary>
  371. /// 设置Cell激活
  372. /// </summary>
  373. /// <param name="row">UltraGridRow</param>
  374. /// <param name="columnName">列名</param>
  375. public static void SetCellActive(this UltraGridRow row, string columnName)
  376. {
  377. row.Cells[columnName].Activate();
  378. }
  379. /// <summary>
  380. /// 设置ActiveRow的指定Cell激活
  381. /// </summary>
  382. /// <param name="grid">UltraGrid</param>
  383. /// <param name="columnName">列名</param>
  384. public static void SetRowActive(this UltraGrid grid, string columnName)
  385. {
  386. grid.ActiveRow.Cells[columnName].Activate();
  387. }
  388. /// <summary>
  389. /// 转换为字符串,null转换为空字符串。
  390. /// </summary>
  391. /// <param name="obj"></param>
  392. /// <returns></returns>
  393. public static string ToString2(this object obj)
  394. {
  395. return obj == null ? "" : obj.ToString();
  396. }
  397. /// <summary>
  398. /// 转换为字符串,null和空字符串转换为0.(针对于数字型的字符串)
  399. /// </summary>
  400. /// <param name="obj"></param>
  401. /// <returns></returns>
  402. public static string ToString3(this object obj)
  403. {
  404. if (obj.ToString2() == "")
  405. obj = "0";
  406. return obj.ToString();
  407. }
  408. }
  409. }