/// <summary> /// 获取条件的比较类型 /// </summary> /// <param name="compareType"></param> /// <returns></returns> internal static IfConditionCompareType GetIfConditionCompareType(string compareType) { IfConditionCompareType icct = IfConditionCompareType.Equal; if (!string.IsNullOrEmpty(compareType)) { switch (compareType.Trim()) { case ">": icct = IfConditionCompareType.GT; break; case ">=": icct = IfConditionCompareType.GTAndEqual; break; case "<": icct = IfConditionCompareType.LT; break; case "<=": icct = IfConditionCompareType.LTAndEqual; break; case "<>": case "!=": icct = IfConditionCompareType.UnEqual; break; case "^=": icct = IfConditionCompareType.StartWith; break; case "$=": icct = IfConditionCompareType.EndWith; break; case "*=": icct = IfConditionCompareType.Contains; break; default: icct = IfConditionCompareType.Equal; break; } } return(icct); }
/// <summary> /// 判断测试条件是否成功 /// </summary> /// <returns></returns> internal virtual bool IsTestSuccess() { //本元素没有条件.所以匹配成功 if (this.Values == null || this.VarExpression == null) { return(true); } //取得条件变量的值 object testValue = this.VarExpression.GetValue(); IfConditionCompareType compare = this.Compare == null ? IfConditionCompareType.Equal : Utility.GetIfConditionCompareType(this.Compare.GetTextValue()); if (Utility.IsNothing(testValue)) { //条件变量的值为null或DBNull.Value. switch (compare) { case IfConditionCompareType.Equal: case IfConditionCompareType.Contains: //如果相等或者包含比较.则比较值列表中是否有空值比较,如果有则认为成立.否则不成立 foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); string value = Utility.IsNothing(obj) ? string.Empty : obj.ToString(); if (value == string.Empty) { return(true); } } return(false); case IfConditionCompareType.UnEqual: //如果不相等比较.则比较值列表中是否有和非空值比较,如果有则认为成立.否则不成立 foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); string value = Utility.IsNothing(obj) ? string.Empty : obj.ToString(); if (value != string.Empty) { return(true); } } return(false); case IfConditionCompareType.LT: case IfConditionCompareType.LTAndEqual: //小于比较,因为null都认为比所有值小.所以等式成立 return(true); default: //其它比较方式.都认为条件不成立 return(false); } } else { //处理表达式 string expression = this.Expression == null ? string.Empty : this.Expression.GetTextValue(); if (!string.IsNullOrEmpty(expression)) { object v = testValue; try { testValue = Evaluator.ExpressionEvaluator.Eval(string.Format(expression, testValue)); } catch { testValue = v; } } bool success; switch (compare) { case IfConditionCompareType.GT: foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); if (!Utility.IsNothing(obj)) { if (Utility.CompareTo(testValue, obj, out success) > 0 && success) { return(true); } } } return(false); case IfConditionCompareType.GTAndEqual: foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); if (!Utility.IsNothing(obj)) { if (Utility.CompareTo(testValue, obj, out success) >= 0 && success) { return(true); } } } return(false); case IfConditionCompareType.LT: foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); if (!Utility.IsNothing(obj)) { if (Utility.CompareTo(testValue, obj, out success) < 0 && success) { return(true); } } } return(false); case IfConditionCompareType.LTAndEqual: foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); if (!Utility.IsNothing(obj)) { if (Utility.CompareTo(testValue, obj, out success) <= 0 && success) { return(true); } } } return(false); case IfConditionCompareType.StartWith: string testValueString1 = testValue.ToString(); foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); if (!Utility.IsNothing(obj)) { string s = obj.ToString(); if (!string.IsNullOrEmpty(s)) { if (testValueString1.StartsWith(s, StringComparison.OrdinalIgnoreCase)) { return(true); } } } } return(false); case IfConditionCompareType.EndWith: string testValueString2 = testValue.ToString(); foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); if (!Utility.IsNothing(obj)) { string s = obj.ToString(); if (!string.IsNullOrEmpty(s)) { if (testValueString2.EndsWith(s, StringComparison.OrdinalIgnoreCase)) { return(true); } } } } return(false); case IfConditionCompareType.Contains: string testValueString3 = testValue.ToString(); foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); if (!Utility.IsNothing(obj)) { string s = obj.ToString(); if (!string.IsNullOrEmpty(s)) { if (testValueString3.IndexOf(s, StringComparison.OrdinalIgnoreCase) != -1) { return(true); } } } } return(false); case IfConditionCompareType.UnEqual: //不等于比较 foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); if (!Utility.IsNothing(obj)) { if (testValue is string || (obj is string && !(testValue is IConvertible))) { //字符串比较.则不区分大小写 if (!string.Equals(obj.ToString(), testValue.ToString(), StringComparison.InvariantCultureIgnoreCase)) { return(true); } } else if (!(testValue is IComparable)) { //优先进行类型比较 if (obj is Type) { if (testValue.GetType() != (Type)obj) { return(true); } } else if (testValue.GetType() == obj.GetType()) { if (testValue != obj) { return(true); } } else { if (Utility.CompareTo(testValue, obj, out success) != 0 && success) { return(true); } } } else { if (Utility.CompareTo(testValue, obj, out success) != 0 && success) { return(true); } } } else { return(true); } } return(false); default: //相等比较 foreach (IExpression exp in this.Values) { object obj = exp.GetValue(); if (!Utility.IsNothing(obj)) { if (testValue is string || (obj is string && !(testValue is IConvertible))) { //字符串比较.则不区分大小写 if (string.Equals(obj.ToString(), testValue.ToString(), StringComparison.InvariantCultureIgnoreCase)) { return(true); } } else if (!(testValue is IComparable)) { //优先进行类型比较 if (obj is Type) { if (testValue.GetType() == (Type)obj) { return(true); } } else if (testValue.GetType() == obj.GetType()) { if (testValue == obj) { return(true); } } else { if (Utility.CompareTo(testValue, obj, out success) == 0 && success) { return(true); } } } else { if (Utility.CompareTo(testValue, obj, out success) == 0 && success) { return(true); } } } } return(false); } } }