/// <summary> /// 计算一个表达式的值 /// </summary> /// <param name="exp">The exp.</param> /// <param name="op">The op.</param> /// <returns></returns> public static object ComputeOne(InnerExpression exp, InnerOperate op) { //"!", "~", "++", "--", "true", "false" object oRet = exp.GetValue(); if (op.OperatorDefine == "!") { return !(Convert.ToBoolean(oRet)); } else if (op.OperatorDefine == "~") { return ~Convert.ToChar(oRet); } else if (op.OperatorDefine == "++" || op.OperatorDefine == "--") { if (!Regex.IsMatch(oRet.ToString(), @"^(\+|\-)?[\d\.]+$", RegexOptions.Compiled)) { throw new InvalidOperationException("数据不支持自增/减运算!"); } else { double odb = double.Parse(oRet.ToString()); return (op.OperatorDefine == "++") ? odb++ : odb--; } } return oRet; }
/// <summary> /// 计算两个表达式的值 /// </summary> public static object ComputeTwo(InnerExpression lExp, InnerOperate op, InnerExpression rExp) { object oRet = ""; object lValue = lExp.GetValue(); object rValue = rExp.GetValue(); //OleDbHelper.AppendToFile("~/debug.txt", "\n" + "Left:" + lValue.GetType().ToString() // + " Right:" + rValue.GetType().ToString()); //Util.Debug(false, lValue.GetType(), rValue.GetType()); //return ""; if (lValue.GetType() != rValue.GetType()) { try { rValue = Convert.ChangeType(rValue, lValue.GetType()); } catch (Exception) { throw new InvalidOperationException(string.Format("{0}与{1}不支持运算,且类型不兼容!", lValue, rValue)); } } string opDef = op.OperatorDefine; #region 处理比较操作 if (ReservedWords.IsStringInRange(opDef, ">", "<", ">=", "<=", "!=", "==", "<>")) { if (!(lValue is IComparable) || !(rValue is IComparable)) { throw new InvalidOperationException(string.Format("{0}与{1}不支持比较!", lValue, rValue)); } IComparable ica = lValue as IComparable; IComparable icb = rValue as IComparable; switch (opDef) { case ">": oRet = (ica.CompareTo(icb) > 0); break; case "<": oRet = (ica.CompareTo(icb) < 0); break; case ">=": oRet = (ica.CompareTo(icb) >= 0); break; case "<=": oRet = (ica.CompareTo(icb) <= 0); break; case "!=": oRet = (ica.CompareTo(icb) != 0); break; case "<>": oRet = (ica.CompareTo(icb) != 0); break; case "==": oRet = (ica.CompareTo(icb) == 0); break; default: break; } } #endregion #region 双目基本运算 if (ReservedWords.IsStringInRange(opDef, "*", "/", "%", "+", "-")) { if (lValue.GetType() == typeof(string)) { #region 字符型 if (op.OperatorDefine == "+") { oRet = String.Concat(lValue.ToString(), rValue.ToString()); } else { oRet = lValue.ToString().Replace(rValue.ToString(), ""); } #endregion } else { #region 数字型 double dba = Convert.ToDouble(lValue); double dbb = Convert.ToDouble(rValue); switch (opDef) { case "*": oRet = (dba * dbb); break; case "/": oRet = (dba / dbb); break; case "%": oRet = (dba % dbb); break; case "+": oRet = (dba + dbb); break; case "-": oRet = (dba - dbb); break; default: break; } #endregion } } #endregion //return string.Format("{0}{1}{2}", lExp.GetValue(), op.OperatorDefine , rExp.GetValue()); return oRet; }
/// <summary> /// 获取改参数的真实值 /// </summary> public object GetValue() { //Util.Debug(false, "Get Param Value:[" + this.ParamDefine + "] Is Fun:" + IsFunction.ToString()); if (this.ParamDefine == null) return null; if (IsExpression == true) { InnerExpression exp = new InnerExpression(this.ParamDefine, GetResourceDependency()); return exp.GetValue(); } else if (IsFunction == true) { int idx = this.ParamDefine.IndexOf('('); string funName = (idx == 0) ? null : this.ParamDefine.Substring(0, idx); //Util.Debug(false, this.ParamDefine.Substring(idx + 1).TrimEnd(')')); InnerFunction fun = new InnerFunction(funName, this.ParamDefine.Substring(idx+1).TrimEnd(')'), this.GetResourceDependency()); return fun.Execute(); } else { #region 字符型 if (this.ParamDefine.StartsWith("\"") && this.ParamDefine.EndsWith("\"")) { return this.ParamDefine.Trim('"'); } else if (this.ParamDefine.StartsWith("'") && this.ParamDefine.EndsWith("'")) { return this.ParamDefine.Trim('\''); } #endregion if (this.ParamDefine.StartsWith("%") && this.ParamDefine.EndsWith("%")) { #region 已定义资源数据 string define = string.Concat("{#", this.ParamDefine, "#}"); if (this.Res != null && Res.IsDefined(define)) { return Res.GetDefinition(define); } else { return ""; } #endregion } else if (this.ParamDefine.StartsWith("$") && this.ParamDefine.EndsWith("$")) { #region 系统标签 SystemTag sysTag = new SystemTag(string.Concat("{#", this.ParamDefine, "#}")); sysTag.SetResourceDependency(GetResourceDependency()); return sysTag.ToString(); #endregion } else { #region 布尔及数字型 if (string.Compare(this.ParamDefine, "true", true) == 0) { return true; } else if (string.Compare(this.ParamDefine, "false", true) == 0) { return false; } else { if (Regex.IsMatch(this.ParamDefine, @"^[\d,]+$", RegexOptions.Compiled)) { try { double dec = double.Parse(this.ParamDefine); return dec; } catch (Exception) { return 0; } } else { return this.ParamDefine; } } #endregion } } }