/// <summary> /// 执行表达式 /// </summary> /// <param name="context">上下文对象</param> /// <returns>运算结果</returns> public object Eval(IDCExpressionContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (this._RootItem == null) { throw new NullReferenceException("this.RootItem"); } object result = this._RootItem.Eval(context); if (result is Array) { Array arr = (Array)result; if (arr.Length > 0) { return(arr.GetValue(0)); } else { return(null); } } return(result); }
/// <summary> /// 执行表达式 /// </summary> /// <param name="context"></param> /// <returns></returns> public override object Eval(IDCExpressionContext context) { if (this.Items != null && this.Items.Count > 0) { return(this.Items[0].Eval(context)); } return(null); }
private string GetStringValue(DCExpressionItem item, IDCExpressionContext context) { if (item == null) { return(null); } object v = item.Eval(context); return(DCExpression.ToString(v)); }
private bool GetBooleanValue(DCExpressionItem item, IDCExpressionContext context, bool defaultValue = false) { if (item == null) { return(defaultValue); } object v = item.Eval(context); return(DCExpression.ToBoolean(v)); }
private double GetDoubleValue(DCExpressionItem item, IDCExpressionContext context, double defaultValue = 0) { if (item == null) { return(defaultValue); } object v = item.Eval(context); return(DCExpression.ToDouble(v, defaultValue)); }
/// <summary> /// 执行表达式 /// </summary> /// <param name="context"></param> /// <returns></returns> public override object Eval(IDCExpressionContext context) { object[] pvs = null; if (_Parameters != null && _Parameters.Count > 0) { pvs = new object[this._Parameters.Count]; for (int iCount = 0; iCount < this._Parameters.Count; iCount++) { pvs[iCount] = this._Parameters[iCount].Eval(context); } } object result = context.ExecuteFunction(this.Name, pvs); return(result); }
/// <summary> /// 执行表达式 /// </summary> /// <param name="context">上下文对象</param> /// <returns>执行结果</returns> public override object Eval(IDCExpressionContext context) { if (this.ValueType == DCValueType.String) { return(this._StringValue); } if (this.ValueType == DCValueType.Number) { return(this._NumberValue); } if (this.ValueType == DCValueType.Boolean) { return(this._BooleanValue); } throw new NotSupportedException(this.ValueType.ToString()); }
/// <summary> /// 执行表达式 /// </summary> /// <param name="context"></param> /// <returns></returns> public virtual object Eval(IDCExpressionContext context) { return(null); }
/// <summary> /// 获得数值比较结果 /// </summary> /// <param name="context"></param> /// <returns></returns> private int GetEqualResult(IDCExpressionContext context) { object v1 = this.Left == null ? null : this.Left.Eval(context); object v2 = this.Rigth == null ? null : this.Rigth.Eval(context); if (v1 == v2) { return(0); } if ((v1 is int || v1 is double) && (v2 is int || v2 is double)) { // 明确进行数字比较 double d1 = Convert.ToDouble(v1); double d2 = Convert.ToDouble(v2); return(d1.CompareTo(d2)); } if (v1 is string && v2 is string) { // 明确进行字符串比较 return(string.Compare((string)v1, (string)v2)); } if (v1 is float && v2 is float) { return(((float)v1).CompareTo((float)v2)); } if (v1 is double && v2 is double) { return(((double)v1).CompareTo((double)v2)); } bool hasContent1 = v1 != null && DBNull.Value.Equals(v1) == false; bool hasContent2 = v2 != null && DBNull.Value.Equals(v2) == false; if (hasContent1 && hasContent2) { try { // 两个数据都不为空 var t1 = v1.GetType(); var t2 = v2.GetType(); if (t1 == t2) { // 类型一致,则进行比较 return(System.Collections.Comparer.Default.Compare(v1, v2)); } // 类型不一样,则设置标准数据类型 var targetVT = (DCValueType)Math.Min((int)GetValueType(t1), (int)GetValueType(t2)); if (targetVT == DCValueType.Boolean) { bool b1 = Convert.ToBoolean(v1); bool b2 = Convert.ToBoolean(v2); return(b1.CompareTo(b2)); } else if (targetVT == DCValueType.Number) { double dbl1 = Convert.ToDouble(v1); double dbl2 = Convert.ToDouble(v2); return(dbl1.CompareTo(dbl2)); } else { string str1 = Convert.ToString(v1); string str2 = Convert.ToString(v2); return(str1.CompareTo(str2)); } } catch (System.Exception ext) { System.Diagnostics.Debug.WriteLine(ext.ToString()); } } return(hasContent1.CompareTo(hasContent2)); }
/// <summary> /// 执行表达式 /// </summary> /// <param name="context"></param> /// <returns></returns> public override object Eval(IDCExpressionContext context) { switch (this.Operator) { #region 逻辑运算 case DCOperatorType.And: { //逻辑与 bool v1 = GetBooleanValue(this.Left, context, false); if (v1 == false) { return(false); } bool v2 = GetBooleanValue(this.Rigth, context, false); return(v2); } case DCOperatorType.Or: { //逻辑或 bool v1 = GetBooleanValue(this.Left, context, false); if (v1) { return(true); } bool v2 = GetBooleanValue(this.Rigth, context, false); return(v2); //return v1 && v2; } #endregion #region 数值比较 case DCOperatorType.Bigger: { // 大于 return(GetEqualResult(context) > 0); } case DCOperatorType.BiggerOrEqual: { // 大于等于 return(GetEqualResult(context) >= 0); } case DCOperatorType.Equal: { // 等于 return(GetEqualResult(context) == 0); } case DCOperatorType.Less: { // 大于等于 return(GetEqualResult(context) < 0); } case DCOperatorType.LessOrEqual: { // 大于等于 return(GetEqualResult(context) <= 0); } case DCOperatorType.Unequal: { // 不等于 return(GetEqualResult(context) != 0); } #endregion #region 数学运算 case DCOperatorType.Plus: { // 加法 double v1 = GetDoubleValue(this.Left, context, 0); double v2 = GetDoubleValue(this.Rigth, context, 0); return(v1 + v2); } case DCOperatorType.Minus: { // 减法 double v1 = GetDoubleValue(this.Left, context, 0); double v2 = GetDoubleValue(this.Rigth, context, 0); return(v1 - v2); } case DCOperatorType.Multi: { // 乘法 double v1 = GetDoubleValue(this.Left, context, 0); double v2 = GetDoubleValue(this.Rigth, context, 0); return(v1 * v2); } case DCOperatorType.Negative: { // 负数 double v1 = 0; if (this.Rigth != null) { v1 = GetDoubleValue(this.Rigth, context, 0); } else if (this.Left != null) { v1 = GetDoubleValue(this.Left, context, 0); } return(-v1); } case DCOperatorType.Division: { // 除法 double v1 = GetDoubleValue(this.Left, context, 0); double v2 = GetDoubleValue(this.Rigth, context, 0); if (v2 == 0) { return(double.NaN); } else { return(v1 / v2); } } case DCOperatorType.Mod: { // 求模 double v1 = GetDoubleValue(this.Left, context, 0); double v2 = GetDoubleValue(this.Rigth, context, 0); if (v2 == 0) { return(double.NaN); } else { return(v1 % v2); } } #endregion default: throw new NotSupportedException(this.Operator.ToString()); } }
/// <summary> /// 执行表达式,根据上下文获取变量值 /// </summary> /// <param name="context">上下文对象</param> /// <returns>变量值</returns> public override object Eval(IDCExpressionContext context) { return(context.GetVariableValue(this.Name)); }