/// <summary> /// For use with objects implementing INamedValueProvider (note, don't call from the INamedValueProvider unless you want to see a StackOverflowException). /// If the sourceName contains ':' then it is split into the /// parts since ':' is assumed to indicate hierarchy. example: foo:bar:fum means get the item bar from foo, and get item fum from bar. /// </summary> /// <param name="sourceName">The name of the value to get</param> /// <param name="sourceObject">The object to get the value from </param> /// <returns>Value if found, otherwise null</returns> public static object GetNamedValue(string sourceName, INamedValueProvider sourceObject) { object sourceValue; if (sourceName.Contains(":")) { string[] sourceParts = sourceName.Split(':'); if (sourceParts[0].ToLower() == "setting") { sourceValue = null; // TODO Settings.GetSetting(sourceParts[sourceParts.Length - 1]); } else { for (int i = 0; i < sourceParts.Length - 1; i++) { object subSourceObject = sourceObject.GetValue(sourceParts[i]); if (subSourceObject is INamedValueProvider) { sourceObject = (INamedValueProvider)subSourceObject; } } sourceValue = sourceObject.GetValue(sourceParts[sourceParts.Length - 1]); } } else { sourceValue = sourceObject.GetValue(sourceName); } return sourceValue; }
public bool Evaluate(INamedValueProvider sourceObject) { object sourceValue = General.GetNamedValue(Source, sourceObject); bool result = false; if (sourceValue == null) { if (Operator == "isnull") { result = true; } } else { if (Operator == "in" || Operator == "notin") { string sourceValueAsString = sourceValue.ToString(); result = sourceValueAsString.Length>0 && Operand.IndexOf(sourceValueAsString, StringComparison.CurrentCultureIgnoreCase) > -1; if (Operator == "notin") { result = !result; } } else { object convertedCompareValue; try { convertedCompareValue = General.ChangeType(Operand, sourceValue.GetType()); } catch (Exception ex) { //this happens when source is enum but not the supplied value //as it can't be compared to enum source it doesn't matter whether supplied value is null or any other value //assign null value as there is no difference. convertedCompareValue = null; } switch (Operator) { case "equals": case "equal": case "eq": case "==": case "=": case "notequal": case "!=": if (sourceValue is IComparable) { if (isDateType(sourceValue, convertedCompareValue)) { TimeSpan ts = (DateTime)sourceValue - (DateTime)convertedCompareValue; result = false; if (ts.Seconds == 0 && ts.Minutes == 0 && ts.Hours == 0 && ts.Days == 0) result = true; } else { if (sourceValue != null && convertedCompareValue != null) { result = ((IComparable)sourceValue.ToString().ToLower()).CompareTo(convertedCompareValue.ToString().ToLower()) == 0; } else { result = ((IComparable)sourceValue).CompareTo(convertedCompareValue) == 0; } } } else { result = sourceValue == convertedCompareValue; } if (Operator == "notequal" || Operator == "!=") { result = !result; } break; case "<": case "lt": case "lessthan": if (isDateType(sourceValue, convertedCompareValue)) { result = false; // Less than zero t1 is less than t2. Zero t1 equals t2. Greater than zero t1 // is greater than t2. if (DateTime.Compare((DateTime)sourceValue,(DateTime)convertedCompareValue) < 0) result = true; } else { if (sourceValue != null && convertedCompareValue != null) { result = ((IComparable)sourceValue.ToString().ToLower()).CompareTo(convertedCompareValue.ToString().ToLower()) < 0; } else { result = ((IComparable)sourceValue).CompareTo(convertedCompareValue) < 0; } } break; case "<=": case "lteq": if (isDateType(sourceValue, convertedCompareValue)) { result = false; if (DateTime.Compare((DateTime)sourceValue, (DateTime)convertedCompareValue) <= 0) result = true; } else { if (sourceValue != null && convertedCompareValue != null) { result = ((IComparable)sourceValue.ToString().ToLower()).CompareTo(convertedCompareValue.ToString().ToLower()) <= 0; } else { result = ((IComparable)sourceValue).CompareTo(convertedCompareValue) <= 0; } } break; case ">": case "gt": case "greaterthan": if (isDateType(sourceValue, convertedCompareValue)) { result = false; if (DateTime.Compare((DateTime)sourceValue, (DateTime)convertedCompareValue) > 0) result = true; } else { if (sourceValue != null && convertedCompareValue != null) { result = ((IComparable)sourceValue.ToString().ToLower()).CompareTo(convertedCompareValue.ToString().ToLower()) > 0; } else { result = ((IComparable)sourceValue).CompareTo(convertedCompareValue) > 0; } } break; case ">=": case "gteq": if (isDateType(sourceValue, convertedCompareValue)) { result = false; if (DateTime.Compare((DateTime)sourceValue, (DateTime)convertedCompareValue) >= 0) result = true; } else { if (sourceValue != null && convertedCompareValue != null) { result = ((IComparable)sourceValue.ToString().ToLower()).CompareTo(convertedCompareValue.ToString().ToLower()) >= 0; } else { result = ((IComparable)sourceValue).CompareTo(convertedCompareValue) >= 0; } } break; } } } return result; }
public static bool EvaluateConditions(List<Condition> conditions, INamedValueProvider sourceObject) { bool? overallResult = null; foreach (Condition condition in conditions) { bool result = condition.Evaluate(sourceObject); if (string.IsNullOrEmpty(condition.Relation) || condition.Relation == "and") { if (overallResult.HasValue) overallResult = overallResult.Value && result; else overallResult = result; } else if (condition.Relation == "or") { if (overallResult.HasValue) { if (result) { overallResult = true; } } else { overallResult = result; } } } return overallResult.HasValue ? overallResult.Value : false; }