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
{
///
/// 附加类
///
public static class ExtendClass
{
///
/// 批量添加参数
///
///
/// 参数
public static void AddRange(this ArrayList list, params string[] args)
{
foreach (string str in args)
{
list.Add(str);
}
}
///
/// 批量添加参数
///
///
/// 参数
public static void AddRange(this List list, params string[] args)
{
foreach (string str in args)
{
list.Add(str);
}
}
///
/// 是否可以转换为整型
///
/// 字符串
/// 是否可以转换
public static bool TryParseInt(this string str)
{
int a = 0;
return int.TryParse(str, out a);
}
///
/// 是否可以转换为双浮点型
///
/// 字符串
/// 是否可以转换
public static bool TryParseDouble(this string str)
{
double a = 0.0D;
return double.TryParse(str, out a);
}
///
/// 是否可以转换为货币型
///
/// 字符串
/// 是否可以转换
public static bool TryParseDecimal(this string str)
{
decimal a = 0.0M;
return decimal.TryParse(str, out a);
}
///
/// 查询
///
/// IQueryable对象
/// 条件筛选表达式
///
public static IQueryable Where(this IQueryable 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;
/////
///// 将输入的计算公式 动态编译,再计算其值
/////
///// 输入的表现公式
/////
//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();
///
/// 将输入的计算公式,通过脚本计算
///
/// 输入的表现公式
///
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;
}
///
/// 计算字符串表达式 如:(2*a-8)/d 或者三元运算 (2*a-8)/d > 0 ? 2*a : (2*a + 1)
///
/// 输入的表现公式
///
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);
}
///
/// 获取Cell实际值
///
/// UltraGridRow
/// 列名
/// 实际值
public static string GetValue(this UltraGridRow row, string columnName)
{
try
{
return row.Cells[columnName].Value.ToString2();
}
catch (Exception ex)
{
throw;
}
}
///
/// 设置Cell实际值
///
/// UltraGridRow
/// 列名
/// 值
public static void SetValue(this UltraGridRow row, string columnName, string value)
{
row.Cells[columnName].Value = value;
}
///
/// 获取Cell显示值
///
/// UltraGridRow
/// 列名
/// 显示值
public static string GetText(this UltraGridRow row, string columnName)
{
return row.Cells[columnName].Text.Trim();
}
///
/// 获取激活行的Cell实际值
///
/// grid
/// 列名
/// 实际值
public static string GetActiveRowValue(this UltraGrid grid, string columnName)
{
return grid.ActiveRow.Cells[columnName].Value.ToString();
}
///
/// 设置激活行的Cell实际值
///
/// UltraGrid
/// 列名
/// 值
public static void SetActiveRowValue(this UltraGrid grid, string columnName, string value)
{
grid.ActiveRow.Cells[columnName].Value = value;
}
///
/// 获取激活行的Cell显示值
///
/// grid
/// 列名
/// 显示值
public static string GetActiveRowText(this UltraGrid grid, string columnName)
{
return grid.ActiveRow.Cells[columnName].Text.ToString();
}
///
/// 设置Cell激活
///
/// UltraGridRow
/// 列名
public static void SetCellActive(this UltraGridRow row, string columnName)
{
row.Cells[columnName].Activate();
}
///
/// 设置ActiveRow的指定Cell激活
///
/// UltraGrid
/// 列名
public static void SetRowActive(this UltraGrid grid, string columnName)
{
grid.ActiveRow.Cells[columnName].Activate();
}
///
/// 转换为字符串,null转换为空字符串。
///
///
///
public static string ToString2(this object obj)
{
return obj == null ? "" : obj.ToString();
}
///
/// 转换为字符串,null和空字符串转换为0.(针对于数字型的字符串)
///
///
///
public static string ToString3(this object obj)
{
if (obj.ToString2() == "")
obj = "0";
return obj.ToString();
}
}
}