protected override object Evaluate(object context, EvaluationContext evalContext) { object left = Left.EvaluateInternal(context, evalContext); object right = Right.EvaluateInternal(context, evalContext); if (NumberUtils.IsNumber(left) && NumberUtils.IsNumber(right)) { return(NumberUtils.Add(left, right)); } else if (left is DateTime && (right is TimeSpan || right is string || NumberUtils.IsNumber(right))) { if (NumberUtils.IsNumber(right)) { right = TimeSpan.FromDays(System.Convert.ToDouble(right)); } else if (right is string) { right = TimeSpan.Parse((string)right); } return((DateTime)left + (TimeSpan)right); } else if (left is String || right is String) { return(left.ToString() + right.ToString()); } else { throw new ArgumentException("Cannot add instances of '" + left.GetType().FullName + "' and '" + right.GetType().FullName + "'."); } }
protected override object Evaluate(object context, BaseNode.EvaluationContext evalContext) { object number = base.Left.EvaluateInternal(context, evalContext); object obj3 = base.Right.EvaluateInternal(context, evalContext); if (NumberUtils.IsNumber(number) && NumberUtils.IsNumber(obj3)) { return(NumberUtils.Add(number, obj3)); } if ((number is DateTime) && (((obj3 is TimeSpan) || (obj3 is string)) || NumberUtils.IsNumber(obj3))) { if (NumberUtils.IsNumber(obj3)) { obj3 = TimeSpan.FromDays(Convert.ToDouble(obj3)); } else if (obj3 is string) { obj3 = TimeSpan.Parse((string)obj3); } return(((DateTime)number) + ((TimeSpan)obj3)); } if (!(number is string) && !(obj3 is string)) { throw new ArgumentException("Cannot add instances of '" + number.GetType().FullName + "' and '" + obj3.GetType().FullName + "'."); } return(number.ToString() + obj3.ToString()); }
/// <summary> /// Returns a value for the arithmetic addition operator node. /// </summary> /// <param name="context">Context to evaluate expressions against.</param> /// <param name="evalContext">Current expression evaluation context.</param> /// <returns>Node's value.</returns> protected override object Get(object context, EvaluationContext evalContext) { var lhs = GetLeftValue(context, evalContext); var rhs = GetRightValue(context, evalContext); if (NumberUtils.IsNumber(lhs) && NumberUtils.IsNumber(rhs)) { return(NumberUtils.Add(lhs, rhs)); } if (lhs is DateTime && (rhs is TimeSpan || rhs is string || NumberUtils.IsNumber(rhs))) { if (NumberUtils.IsNumber(rhs)) { rhs = TimeSpan.FromDays(Convert.ToDouble(rhs)); } else if (rhs is string) { rhs = TimeSpan.Parse((string)rhs); } return((DateTime)lhs + (TimeSpan)rhs); } if (lhs is String || rhs is String) { return(string.Concat(lhs, rhs)); } if ((lhs is IList || lhs is ISet) && (rhs is IList || rhs is ISet)) { ISet leftset = new HybridSet(lhs as ICollection); ISet rightset = new HybridSet(rhs as ICollection); return(leftset.Union(rightset)); } if (lhs is IDictionary && rhs is IDictionary) { ISet leftset = new HybridSet(((IDictionary)lhs).Keys); ISet rightset = new HybridSet(((IDictionary)rhs).Keys); var unionset = leftset.Union(rightset); IDictionary result = new Hashtable(unionset.Count); foreach (var key in unionset) { if (leftset.Contains(key)) { result.Add(key, ((IDictionary)lhs)[key]); } else { result.Add(key, ((IDictionary)rhs)[key]); } } return(result); } throw new ArgumentException("Cannot add instances of '" + lhs.GetType().FullName + "' and '" + rhs.GetType().FullName + "'."); }
public void Add() { Assert.AreEqual(5, NumberUtils.Add(2, 3)); try { NumberUtils.Add(2, "3"); Assert.Fail(); } catch(ArgumentException) {} }
/// <summary> /// Returns the sum of the numeric values in the source collection. /// </summary> /// <param name="source"> /// The source collection to process. /// </param> /// <param name="args"> /// Ignored. /// </param> /// <returns> /// The sum of the numeric values in the source collection. /// </returns> public object Process(ICollection source, object[] args) { object total = 0d; foreach (object item in source) { if (item != null) { if (NumberUtils.IsNumber(item)) { total = NumberUtils.Add(total, item); } else { throw new ArgumentException("Sum can only be calculated for a collection of numeric values."); } } } return(total); }
/// <summary> /// Returns the average of the numeric values in the source collection. /// </summary> /// <param name="source"> /// The source collection to process. /// </param> /// <param name="args"> /// Ignored. /// </param> /// <returns> /// The average of the numeric values in the source collection. /// </returns> public object Execute(ICollection source, object[] args) { var n = 0; object total = 0d; foreach (var item in source) { if (item != null) { if (NumberUtils.IsNumber(item)) { total = NumberUtils.Add(total, item); n++; } else { throw new ArgumentException("Average can only be calculated for a collection of numeric values."); } } } return(NumberUtils.Divide(total, n)); }