| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- using Infragistics.Win.UltraWinGrid;
- using Microsoft.CSharp;
- using System.CodeDom.Compiler;
- using System.Windows.Forms;
- using System.Reflection;
- using Core.Mes.Client.Comm.Tool;
- namespace System
- {
- /// <summary>
- /// 附加类
- /// </summary>
- public static class ExtendClass
- {
- /// <summary>
- /// 批量添加参数
- /// </summary>
- /// <param name="list"></param>
- /// <param name="args">参数</param>
- public static void AddRange(this ArrayList list, params string[] args)
- {
- foreach (string str in args)
- {
- list.Add(str);
- }
- }
- /// <summary>
- /// 批量添加参数
- /// </summary>
- /// <param name="list"></param>
- /// <param name="args">参数</param>
- public static void AddRange(this List<string> list, params string[] args)
- {
- foreach (string str in args)
- {
- list.Add(str);
- }
- }
- /// <summary>
- /// 是否可以转换为整型
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>是否可以转换</returns>
- public static bool TryParseInt(this string str)
- {
- int a = 0;
- return int.TryParse(str, out a);
- }
- /// <summary>
- /// 是否可以转换为双浮点型
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>是否可以转换</returns>
- public static bool TryParseDouble(this string str)
- {
- double a = 0.0D;
- return double.TryParse(str, out a);
- }
- /// <summary>
- /// 是否可以转换为货币型
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>是否可以转换</returns>
- public static bool TryParseDecimal(this string str)
- {
- decimal a = 0.0M;
- return decimal.TryParse(str, out a);
- }
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="qRows">IQueryable对象</param>
- /// <param name="filterExpression">条件筛选表达式</param>
- /// <returns></returns>
- public static IQueryable<UltraGridRow> Where(this IQueryable<UltraGridRow> qRows,
- string filterExpression)
- {
- string[] strings = filterExpression.Trim().Split(' ');
- string[] strSings = new string[] { "=", ">", ">=", "<", "<=", "!=", "<>" };
- string field = strings[0].Trim();
- string sing = strings[1].Trim();
- string value = strings[2].Trim(' ', '\'');
- if (strSings.Contains(sing) == false) throw new Exception("表达式错误!");
- switch (sing)
- {
- case "=":
- qRows = from a in qRows
- where a.Cells[field].Value.ToString() == value
- select a;
- break;
- case ">":
- qRows = qRows.Where(a =>
- a.Cells[field].Value.ToString() != ""
- && a.Cells[field].Value.ToString().TryParseDecimal()
- && decimal.Parse(a.Cells[field].Value.ToString()) > decimal.Parse(value));
- break;
- case ">=":
- qRows = qRows.Where(a =>
- a.Cells[field].Value.ToString() != ""
- && a.Cells[field].Value.ToString().TryParseDecimal()
- && decimal.Parse(a.Cells[field].Value.ToString()) >= decimal.Parse(value));
- break;
- case "<":
- qRows = qRows.Where(a =>
- a.Cells[field].Value.ToString() != ""
- && a.Cells[field].Value.ToString().TryParseDecimal()
- && decimal.Parse(a.Cells[field].Value.ToString()) < decimal.Parse(value));
- break;
- case "<=":
- qRows = qRows.Where(a =>
- a.Cells[field].Value.ToString() != ""
- && a.Cells[field].Value.ToString().TryParseDecimal()
- && decimal.Parse(a.Cells[field].Value.ToString()) <= decimal.Parse(value));
- break;
- case "!=":
- case "<>":
- qRows = from a in qRows
- where a.Cells[field].Value.ToString() != value
- select a;
- break;
- }
- return qRows;
- }
- private static CSharpCodeProvider provider;
- private static CompilerParameters parameter;
- ///// <summary>
- ///// 将输入的计算公式 动态编译,再计算其值
- ///// </summary>
- ///// <param name="formula">输入的表现公式</param>
- ///// <returns></returns>
- //public static decimal? CompileFormula(this string formula)
- //{
- // //1942.57*(Axc^0.2)/(517^0.9) 8^2^4
- // formula = ReplacePowSign(formula, 0);
- // if(provider == null)
- // {
- // provider = new CSharpCodeProvider();
- // parameter = new CompilerParameters();
- // parameter.ReferencedAssemblies.Add("System.dll");
- // parameter.GenerateExecutable = false;
- // parameter.GenerateInMemory = true;
- // }
- // CompilerResults result = provider.CompileAssemblyFromSource(parameter,
- // CreateCode(formula));//将你的式子放在这里
- // if (result.Errors.Count > 0)
- // {
- // return null;
- // }
- // else
- // {
- // Assembly assembly = result.CompiledAssembly;
- // Type AType = assembly.GetType("ANameSpace.AClass");
- // MethodInfo method = AType.GetMethod("AFunc");
- // decimal count = decimal.Parse(method.Invoke(null, null).ToString());
- // return count;
- // }
- //}
- private static Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
- /// <summary>
- /// 将输入的计算公式,通过脚本计算
- /// </summary>
- /// <param name="formula">输入的表现公式</param>
- /// <returns></returns>
- public static decimal? CompileFormula(this string formula)
- {
- //1942.57*(Axc^0.2)/(517^0.9) 8^2^4
- formula = powSign(formula);
- //1942.57*(Math.Pow((double)1, (double)0.2))/(Math.Pow((double)517, (double)0.9))
- decimal? value = null;
- try
- {
- value = decimal.Parse(Microsoft.JScript.Eval.JScriptEvaluate(formula, ve).ToString());
- }
- catch (Exception ex)
- {
- return null;
- }
- return value;
- }
- /// <summary>
- /// 计算字符串表达式 如:(2*a-8)/d 或者三元运算 (2*a-8)/d > 0 ? 2*a : (2*a + 1)
- /// </summary>
- /// <param name="formula">输入的表现公式</param>
- /// <returns></returns>
- public static string Eval(this string express)
- {
- //1942.57*(Axc^0.2)/(517^0.9) 8^2^4
- express = powSign(express);
- //1942.57*(Math.Pow((double)1, (double)0.2))/(Math.Pow((double)517, (double)0.9))
- string result = "";
- try
- {
- result = Microsoft.JScript.Eval.JScriptEvaluate(express, ve).ToString();
- }
- catch (Exception ex)
- {
- return null;
- }
- return result;
- }
- private static string powSign(string express)
- {
- if (!express.Contains("^")) return express;
- //2+(6+2)^2+2^2
- String sings = "+-*/()^";
- int index = express.IndexOf("^");
- char[] strChars = express.Substring(0, index).ToCharArray();
- char[] str2Chars = express.Substring(index + 1).ToCharArray();
- StringBuilder strBld = new StringBuilder();
- strBld.Append(strChars);
- int khCnt = 0;
- bool isKh = false;
- for (int i = strChars.Length - 1; i >= 0; i--)
- {
- char strChar = strChars[i];
- if (strChar == ')')
- {
- if (i == strChars.Length - 1)
- {
- isKh = true;
- }
- khCnt++;
- }
- else if (strChar == '(')
- {
- khCnt--;
- }
- if (isKh)
- {
- if (khCnt == 0)
- {
- strBld.Insert(i, "Math.pow(");
- break;
- }
- }
- else
- {
- if (i == 0)
- {
- strBld.Insert(i, "Math.pow(");
- break;
- }
- else if (sings.IndexOf(strChar) > -1)
- {
- strBld.Insert(i + 1, "Math.pow(");
- break;
- }
- }
- }
- //2+(6+2)^2+2^2
- StringBuilder str2Bld = new StringBuilder();
- str2Bld.Append(str2Chars);
- khCnt = 0;
- isKh = false;
- for (int i = 0; i < str2Chars.Length; i++)
- {
- char strChar = str2Chars[i];
- if (i == 0)
- {
- str2Bld.Insert(0, ", ");
- if (strChar == '(')
- {
- isKh = true;
- }
- }
- if (strChar == '(')
- {
- khCnt++;
- }
- else if (strChar == ')')
- {
- khCnt--;
- }
- if (isKh)
- {
- if (khCnt == 0)
- {
- str2Bld.Insert(i + 3, ")");
- break;
- }
- }
- else
- {
- if (i == str2Chars.Length - 1)
- {
- str2Bld.Insert(i + 3, ")");
- break;
- }
- else if (sings.IndexOf(strChar) > -1)
- {
- str2Bld.Insert(i + 2, ")");
- break;
- }
- }
- }
- express = strBld.ToString() + str2Bld.ToString();
- return powSign(express);
- }
- /// <summary>
- /// 获取Cell实际值
- /// </summary>
- /// <param name="row">UltraGridRow</param>
- /// <param name="columnName">列名</param>
- /// <returns>实际值</returns>
- public static string GetValue(this UltraGridRow row, string columnName)
- {
- try
- {
- return row.Cells[columnName].Value.ToString2();
- }
- catch (Exception ex)
- {
- throw;
- }
- }
- /// <summary>
- /// 设置Cell实际值
- /// </summary>
- /// <param name="row">UltraGridRow</param>
- /// <param name="columnName">列名</param>
- /// <param name="value">值</param>
- public static void SetValue(this UltraGridRow row, string columnName, string value)
- {
- row.Cells[columnName].Value = value;
- }
- /// <summary>
- /// 获取Cell显示值
- /// </summary>
- /// <param name="row">UltraGridRow</param>
- /// <param name="columnName">列名</param>
- /// <returns>显示值</returns>
- public static string GetText(this UltraGridRow row, string columnName)
- {
- return row.Cells[columnName].Text.Trim();
- }
- /// <summary>
- /// 获取激活行的Cell实际值
- /// </summary>
- /// <param name="grid">grid</param>
- /// <param name="columnName">列名</param>
- /// <returns>实际值</returns>
- public static string GetActiveRowValue(this UltraGrid grid, string columnName)
- {
- return grid.ActiveRow.Cells[columnName].Value.ToString();
- }
- /// <summary>
- /// 设置激活行的Cell实际值
- /// </summary>
- /// <param name="grid">UltraGrid</param>
- /// <param name="columnName">列名</param>
- /// <param name="value">值</param>
- public static void SetActiveRowValue(this UltraGrid grid, string columnName, string value)
- {
- grid.ActiveRow.Cells[columnName].Value = value;
- }
- /// <summary>
- /// 获取激活行的Cell显示值
- /// </summary>
- /// <param name="grid">grid</param>
- /// <param name="columnName">列名</param>
- /// <returns>显示值</returns>
- public static string GetActiveRowText(this UltraGrid grid, string columnName)
- {
- return grid.ActiveRow.Cells[columnName].Text.ToString();
- }
- /// <summary>
- /// 设置Cell激活
- /// </summary>
- /// <param name="row">UltraGridRow</param>
- /// <param name="columnName">列名</param>
- public static void SetCellActive(this UltraGridRow row, string columnName)
- {
- row.Cells[columnName].Activate();
- }
- /// <summary>
- /// 设置ActiveRow的指定Cell激活
- /// </summary>
- /// <param name="grid">UltraGrid</param>
- /// <param name="columnName">列名</param>
- public static void SetRowActive(this UltraGrid grid, string columnName)
- {
- grid.ActiveRow.Cells[columnName].Activate();
- }
- /// <summary>
- /// 转换为字符串,null转换为空字符串。
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string ToString2(this object obj)
- {
- return obj == null ? "" : obj.ToString();
- }
- /// <summary>
- /// 转换为字符串,null和空字符串转换为0.(针对于数字型的字符串)
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string ToString3(this object obj)
- {
- if (obj.ToString2() == "")
- obj = "0";
- return obj.ToString();
- }
- }
- }
|