Ejemplo n.º 1
0
        /// <summary>
        /// Returns a value for the arithmetic multiplication 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.Multiply(lhs, rhs);
            }

            if (lhs is IList ||
                lhs is ISet)
            {
                ISet leftset = new HybridSet(lhs as ICollection);
                ISet rightset;
                if (rhs is IList ||
                    rhs is ISet)
                {
                    rightset = new HybridSet(rhs as ICollection);
                }
                else if (rhs is IDictionary)
                {
                    rightset = new HybridSet(((IDictionary)rhs).Keys);
                }
                else
                {
                    throw new ArgumentException("Cannot subtract instances of '"
                                                + lhs.GetType().FullName
                                                + "' and '"
                                                + rhs.GetType().FullName
                                                + "'.");
                }
                return leftset.Intersect(rightset);
            }
            if (lhs is IDictionary)
            {
                ISet leftset = new HybridSet(((IDictionary)lhs).Keys);
                ISet rightset;
                if (rhs is IList || rhs is ISet)
                {
                    rightset = new HybridSet(rhs as ICollection);
                }
                else if (rhs is IDictionary)
                {
                    rightset = new HybridSet(((IDictionary)rhs).Keys);
                }
                else
                {
                    throw new ArgumentException("Cannot subtract instances of '"
                                                + lhs.GetType().FullName
                                                + "' and '"
                                                + rhs.GetType().FullName
                                                + "'.");
                }
                IDictionary result = new Hashtable(rightset.Count);
                foreach (var key in leftset.Intersect(rightset))
                {
                    result.Add(key, ((IDictionary)lhs)[key]);
                }
                return result;
            }
            throw new ArgumentException("Cannot multiply instances of '"
                                        + lhs.GetType().FullName
                                        + "' and '"
                                        + rhs.GetType().FullName
                                        + "'.");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a value for the arithmetic multiplication 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)
        {
            object left  = GetLeftValue(context, evalContext);
            object right = GetRightValue(context, evalContext);

            if (NumberUtils.IsNumber(left) && NumberUtils.IsNumber(right))
            {
                return(NumberUtils.Multiply(left, right));
            }
            else if (NumberUtils.IsNumber(left) && NumberUtils.TryConvertTo(ref right, ref left))
            {
                return(NumberUtils.Multiply(left, right));
            }
            else if (NumberUtils.IsNumber(right) && NumberUtils.TryConvertTo(ref left, ref right))
            {
                return(NumberUtils.Multiply(left, right));
            }
            else if (left is IList || left is ISet)
            {
                ISet leftset = new HybridSet(left as ICollection);
                ISet rightset;
                if (right is IList || right is ISet)
                {
                    rightset = new HybridSet(right as ICollection);
                }
                else if (right is IDictionary)
                {
                    rightset = new HybridSet(((IDictionary)right).Keys);
                }
                else
                {
                    throw new ArgumentException("Cannot subtract instances of '"
                                                + left.GetType().FullName
                                                + "' and '"
                                                + right.GetType().FullName
                                                + "'.");
                }
                return(leftset.Intersect(rightset));
            }
            else if (left is IDictionary)
            {
                ISet leftset = new HybridSet(((IDictionary)left).Keys);
                ISet rightset;
                if (right is IList || right is ISet)
                {
                    rightset = new HybridSet(right as ICollection);
                }
                else if (right is IDictionary)
                {
                    rightset = new HybridSet(((IDictionary)right).Keys);
                }
                else
                {
                    throw new ArgumentException("Cannot subtract instances of '"
                                                + left.GetType().FullName
                                                + "' and '"
                                                + right.GetType().FullName
                                                + "'.");
                }
                IDictionary result = new Hashtable(rightset.Count);
                foreach (object key in leftset.Intersect(rightset))
                {
                    result.Add(key, ((IDictionary)left)[key]);
                }
                return(result);
            }
            else
            {
                throw new ArgumentException("Cannot multiply instances of '"
                                            + left.GetType().FullName
                                            + "' and '"
                                            + right.GetType().FullName
                                            + "'.");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a value for the arithmetic multiplication 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.Multiply(lhs, rhs));
            }

            if (lhs is IList ||
                lhs is ISet)
            {
                ISet leftset = new HybridSet(lhs as ICollection);
                ISet rightset;
                if (rhs is IList ||
                    rhs is ISet)
                {
                    rightset = new HybridSet(rhs as ICollection);
                }
                else if (rhs is IDictionary)
                {
                    rightset = new HybridSet(((IDictionary)rhs).Keys);
                }
                else
                {
                    throw new ArgumentException("Cannot subtract instances of '"
                                                + lhs.GetType().FullName
                                                + "' and '"
                                                + rhs.GetType().FullName
                                                + "'.");
                }
                return(leftset.Intersect(rightset));
            }
            if (lhs is IDictionary)
            {
                ISet leftset = new HybridSet(((IDictionary)lhs).Keys);
                ISet rightset;
                if (rhs is IList || rhs is ISet)
                {
                    rightset = new HybridSet(rhs as ICollection);
                }
                else if (rhs is IDictionary)
                {
                    rightset = new HybridSet(((IDictionary)rhs).Keys);
                }
                else
                {
                    throw new ArgumentException("Cannot subtract instances of '"
                                                + lhs.GetType().FullName
                                                + "' and '"
                                                + rhs.GetType().FullName
                                                + "'.");
                }
                IDictionary result = new Hashtable(rightset.Count);
                foreach (var key in leftset.Intersect(rightset))
                {
                    result.Add(key, ((IDictionary)lhs)[key]);
                }
                return(result);
            }
            throw new ArgumentException("Cannot multiply instances of '"
                                        + lhs.GetType().FullName
                                        + "' and '"
                                        + rhs.GetType().FullName
                                        + "'.");
        }