示例#1
0
        public object Evaluate(string expression)
        {
            ExpressionNode node = ExpressionParser.Parse(expression, false);

            if (this.Items.Count == 0 || ExpressionNode.GetNodes <AggregateNode>(node).Count == 0)
            {
                return((object)null);
            }
            List <NameNode>  nodes            = ExpressionNode.GetNodes <NameNode>(node);
            StringCollection stringCollection = new StringCollection();

            foreach (NameNode nameNode in nodes)
            {
                if (!stringCollection.Contains(nameNode.Name))
                {
                    stringCollection.Add(nameNode.Name);
                }
            }
            ExpressionContext context = ExpressionContext.GetContext(Thread.CurrentThread.ManagedThreadId);

            context.Clear();
            for (int index = 0; index < stringCollection.Count; ++index)
            {
                if (context.ContainsKey(stringCollection[index]))
                {
                    context[stringCollection[index]] = this.Items[0][stringCollection[index]];
                }
                else
                {
                    context.Add(stringCollection[index], this.Items[0][stringCollection[index]]);
                }
            }
            return(node.Eval((object)new AggregateItems <T>((IEnumerable <T>) this.Items), (object)context));
        }
示例#2
0
 private bool PerformExpressionFilter(TDataItem item)
 {
     if (this.filterContext.Count == 0)
     {
         return(true);
     }
     try
     {
         lock (this.syncobjs)
         {
             int managedThreadId = Thread.CurrentThread.ManagedThreadId;
             ExpressionContext expressionContext = managedThreadId != this.mainThreadId ? ExpressionContext.GetContext(managedThreadId) : ExpressionContext.Context;
             expressionContext.Clear();
             expressionContext.CaseSensitive = this.CaseSensitive;
             for (int index1 = 0; index1 < this.filterContext.Count; ++index1)
             {
                 string index2 = this.filterContext[index1];
                 object obj    = this.GetFieldValue(item, index2);
                 if (obj is Enum)
                 {
                     obj = (object)Convert.ToInt32(obj);
                 }
                 if (expressionContext.ContainsKey(index2))
                 {
                     expressionContext[index2] = obj;
                 }
                 else
                 {
                     expressionContext.Add(index2, obj);
                 }
             }
             object obj1 = (managedThreadId != this.mainThreadId ? this.GetFilterNode(managedThreadId) : this.filterNode).Eval((object)null, (object)expressionContext);
             if (obj1 is bool)
             {
                 return((bool)obj1);
             }
         }
     }
     catch (Exception ex)
     {
         throw new FilterExpressionException("Invalid filter expression.", ex);
     }
     return(false);
 }
示例#3
0
        /// <summary>
        /// Evaluates the specified expression.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <param name="items">The items.</param>
        /// <returns></returns>
        public object Evaluate(string expression, IEnumerable <TDataItem> items)
        {
            ExpressionNode  node      = ExpressionParser.Parse(expression, this.CaseSensitive);
            List <NameNode> nameNodes = ExpressionNode.GetNodes <NameNode>(node);

            StringCollection contextProperties = new StringCollection();

            foreach (NameNode nameNode in nameNodes)
            {
                if (!contextProperties.Contains(nameNode.Name))
                {
                    contextProperties.Add(nameNode.Name);
                }
            }

            IEnumerator <TDataItem> e = items.GetEnumerator();

            e.MoveNext();

            ExpressionContext context = ExpressionContext.Context;

            context.Clear();
            for (int i = 0; i < contextProperties.Count; i++)
            {
                if (context.ContainsKey(contextProperties[i]))
                {
                    context[contextProperties[i]] = e.Current[contextProperties[i]];
                }
                else
                {
                    context.Add(contextProperties[i], e.Current[contextProperties[i]]);
                }
            }

            if (ExpressionNode.GetNodes <AggregateNode>(node).Count > 0)
            {
                return(node.Eval(new AggregateItems <TDataItem>(items), context));
            }

            return(node.Eval(null, context));
        }
示例#4
0
        private bool PerformExpressionFilter(TDataItem item)
        {
            if (this.filterContext.Count == 0)
            {
                return(false);
            }

            try
            {
                ExpressionContext context = ExpressionContext.Context;
                context.Clear();
                for (int i = 0; i < this.filterContext.Count; i++)
                {
                    string fieldName = this.filterContext[i];
                    if (context.ContainsKey(fieldName))
                    {
                        context[fieldName] = item[fieldName];
                    }
                    else
                    {
                        context.Add(fieldName, item[fieldName]);
                    }
                }

                object result = this.filterNode.Eval(null, context);
                if (result is bool)
                {
                    return((bool)result);
                }
            }
            catch (Exception ex)
            {
                throw new FilterExpressionException("Invalid filter expression.", ex);
            }

            return(false);
        }
示例#5
0
        /// <summary>
        /// Evaluates the specified expression.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public object Evaluate(string expression)
        {
            ExpressionNode node = ExpressionParser.Parse(expression, false);

            if (this.Items.Count == 0 || ExpressionNode.GetNodes <AggregateNode>(node).Count == 0)
            {
                return(null);
            }

            List <NameNode>  nameNodes         = ExpressionNode.GetNodes <NameNode>(node);
            StringCollection contextProperties = new StringCollection();

            foreach (NameNode nameNode in nameNodes)
            {
                if (!contextProperties.Contains(nameNode.Name))
                {
                    contextProperties.Add(nameNode.Name);
                }
            }

            ExpressionContext context = ExpressionContext.Context;

            context.Clear();
            for (int i = 0; i < contextProperties.Count; i++)
            {
                if (context.ContainsKey(contextProperties[i]))
                {
                    context[contextProperties[i]] = this.Items[0][contextProperties[i]];
                }
                else
                {
                    context.Add(contextProperties[i], this.Items[0][contextProperties[i]]);
                }
            }

            return(node.Eval(new AggregateItems <T>(this.Items), context));
        }
示例#6
0
        /// <summary>
        /// Try to evaluate the specified expression.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <param name="items">The items.</param>
        /// <param name="index">Index of item, which the result will be calculated for</param>
        /// <param name="result">Expression result</param>
        /// <returns></returns>
        internal bool TryEvaluate(string expression, IEnumerable <TDataItem> items, int index, out object result)
        {
            result = null;

            ExpressionNode node = null;

            if (!ExpressionParser.TryParse(expression, this.CaseSensitive, out node))
            {
                return(false);
            }

            List <NameNode> nameNodes = ExpressionNode.GetNodes <NameNode>(node);

            StringCollection contextProperties = new StringCollection();

            foreach (NameNode nameNode in nameNodes)
            {
                if (!contextProperties.Contains(nameNode.Name))
                {
                    contextProperties.Add(nameNode.Name);
                }
            }

            IEnumerator <TDataItem> e = items.GetEnumerator();

            while (index >= 0)
            {
                if (!e.MoveNext())
                {
                    throw new IndexOutOfRangeException();
                }
                index--;
            }

            if (e.Current == null)
            {
                return(false);
            }

            ExpressionContext context = ExpressionContext.Context;

            context.Clear();

            for (int i = 0; i < contextProperties.Count; i++)
            {
                if (e.Current.IndexOf(contextProperties[i]) < 0)
                {
                    return(false);
                }
                if (context.ContainsKey(contextProperties[i]))
                {
                    context[contextProperties[i]] = e.Current[contextProperties[i]];
                }
                else
                {
                    context.Add(contextProperties[i], e.Current[contextProperties[i]]);
                }
            }

            try
            {
                if (ExpressionNode.GetNodes <AggregateNode>(node).Count > 0)
                {
                    result = node.Eval(new AggregateItems <TDataItem>(items), context);
                }
                else
                {
                    result = node.Eval(null, context);
                }
            }
            catch
            {
                return(false);
            }

            return(true);
        }
示例#7
0
        public bool TryEvaluate(
            string expression,
            IEnumerable <TDataItem> items,
            int index,
            out object result)
        {
            result = (object)null;
            ExpressionNode expressionNode = (ExpressionNode)null;

            if (!ExpressionParser.TryParse(expression, this.CaseSensitive, out expressionNode))
            {
                return(false);
            }
            List <NameNode>  nodes            = ExpressionNode.GetNodes <NameNode>(expressionNode);
            StringCollection stringCollection = new StringCollection();

            foreach (NameNode nameNode in nodes)
            {
                if (!stringCollection.Contains(nameNode.Name))
                {
                    stringCollection.Add(nameNode.Name);
                }
            }
            IEnumerator <TDataItem> enumerator = items.GetEnumerator();

            for (; index >= 0; --index)
            {
                if (!enumerator.MoveNext())
                {
                    throw new IndexOutOfRangeException();
                }
            }
            if ((object)enumerator.Current == null)
            {
                return(false);
            }
            ExpressionContext context = ExpressionContext.Context;

            context.Clear();
            for (int index1 = 0; index1 < stringCollection.Count; ++index1)
            {
                if (enumerator.Current.IndexOf(stringCollection[index1]) < 0)
                {
                    return(false);
                }
                if (context.ContainsKey(stringCollection[index1]))
                {
                    context[stringCollection[index1]] = enumerator.Current[stringCollection[index1]];
                }
                else
                {
                    context.Add(stringCollection[index1], enumerator.Current[stringCollection[index1]]);
                }
            }
            try
            {
                result = ExpressionNode.GetNodes <AggregateNode>(expressionNode).Count <= 0 ? expressionNode.Eval((object)null, (object)context) : expressionNode.Eval((object)new AggregateItems <TDataItem>(items), (object)context);
            }
            catch
            {
                return(false);
            }
            return(true);
        }
示例#8
0
        public object Evaluate(string expression, IEnumerable <TDataItem> items)
        {
            ExpressionNode   node             = ExpressionParser.Parse(expression, this.CaseSensitive);
            List <NameNode>  nodes            = ExpressionNode.GetNodes <NameNode>(node);
            StringCollection stringCollection = new StringCollection();

            foreach (NameNode nameNode in nodes)
            {
                if (!stringCollection.Contains(nameNode.Name))
                {
                    stringCollection.Add(nameNode.Name);
                }
            }
            IEnumerator <TDataItem> enumerator = items.GetEnumerator();

            enumerator.MoveNext();
            ExpressionContext context = ExpressionContext.Context;

            context.Clear();
            for (int index = 0; index < stringCollection.Count; ++index)
            {
                if (!RadCollectionView <TDataItem> .isDesignMode.HasValue)
                {
                    Process currentProcess = Process.GetCurrentProcess();
                    RadCollectionView <TDataItem> .isDesignMode = new bool?(currentProcess.ProcessName == "devenv");
                    currentProcess.Dispose();
                }
                if (RadCollectionView <TDataItem> .isDesignMode.HasValue)
                {
                    if (RadCollectionView <TDataItem> .isDesignMode.Value)
                    {
                        try
                        {
                            if (context.ContainsKey(stringCollection[index]))
                            {
                                context[stringCollection[index]] = enumerator.Current[stringCollection[index]];
                                continue;
                            }
                            context.Add(stringCollection[index], enumerator.Current[stringCollection[index]]);
                            continue;
                        }
                        catch (Exception ex)
                        {
                            int num = (int)MessageBox.Show(string.Format("Error evaluating expression: {0}", (object)ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            return((object)null);
                        }
                    }
                }
                if (context.ContainsKey(stringCollection[index]))
                {
                    context[stringCollection[index]] = enumerator.Current[stringCollection[index]];
                }
                else
                {
                    context.Add(stringCollection[index], enumerator.Current[stringCollection[index]]);
                }
            }
            if (ExpressionNode.GetNodes <AggregateNode>(node).Count > 0)
            {
                return(node.Eval((object)new AggregateItems <TDataItem>(items), (object)context));
            }
            return(node.Eval((object)null, (object)context));
        }