Ejemplo n.º 1
0
        /// <summary>
        /// Evaluates a simple expression, given via the parameter <paramref name="parameter"/>.
        /// </summary>
        /// <remarks>
        /// This converter will often be used in XAML files. Note that in XAML, an attribute beginning with a <c>'{'</c> character
        /// is interpreted as an invocation of a markup extension. So the expression "{0} + 5" must be escaped like this:
        /// <c>"{}{0} + 5"</c>. Note also that the boolean AND operator (<c>"&&"</c>) must be escaped too like this: <c>"{}{0} &amp;&amp; true"</c>.
        /// </remarks>
        /// <param name="values">The values used for the variables {0} .. {n}.</param>
        /// <param name="targetType">Type to that the evaluated result should be converted.</param>
        /// <param name="parameter">String containing the expression. Variables can be accessed via numbers in
        /// curly braces, for example "!({0} || {2})". The variables are mapped to the values specified by
        /// the <paramref name="values"/> array.</param>
        /// <param name="result">Will return the evaluated result of the given <paramref name="targetType"/>.</param>
        public bool Convert(IDataDescriptor[] values, Type targetType, object parameter, out object result)
        {
            result = null;
            string expression = parameter as string;

            if (string.IsNullOrEmpty(expression))
            {
                return(false);
            }
            try
            {
                // We're using an expression parser from "devilplusplus", "C# Eval function"
                // See http://www.codeproject.com/KB/dotnet/Expr.aspx
                // The parser was slightly adapted to our needs:
                // - To access a variable, the variable identifier has to be written in curly braces, for example:
                //   {0} + {1}

                Parser    ep                = new Parser();
                Evaluator evaluator         = new Evaluator();
                ParameterVariableHolder pvh = new ParameterVariableHolder();

                // The used expression parser supports access to static functions for those of the parameters whose type is a class.
                // We could add classes here like the code commented out below. To access a static member on the string class,
                // the expression could be for example: {string}.{Empty}
                // For now, we don't need this functionality, so we don't add types (Albert, 2009-04-22).

                //pvh.Parameters["char"] = new Parameter(typeof(char));
                //pvh.Parameters["sbyte"] = new Parameter(typeof(sbyte));
                //pvh.Parameters["byte"] = new Parameter(typeof(byte));
                //pvh.Parameters["short"] = new Parameter(typeof(short));
                //pvh.Parameters["ushort"] = new Parameter(typeof(ushort));
                //pvh.Parameters["int"] = new Parameter(typeof(int));
                //pvh.Parameters["uint"] = new Parameter(typeof(uint));
                //pvh.Parameters["long"] = new Parameter(typeof(string));
                //pvh.Parameters["ulong"] = new Parameter(typeof(ulong));
                //pvh.Parameters["float"] = new Parameter(typeof(float));
                //pvh.Parameters["double"] = new Parameter(typeof(double));
                //pvh.Parameters["decimal"] = new Parameter(typeof(decimal));
                //pvh.Parameters["DateTime"] = new Parameter(typeof(DateTime));
                //pvh.Parameters["string"] = new Parameter(typeof(string));

                //pvh.Parameters["Guid"] = new Parameter(typeof(Guid));

                //pvh.Parameters["Convert"] = new Parameter(typeof(Convert));
                //pvh.Parameters["Math"] = new Parameter(typeof(Math));
                //pvh.Parameters["Array"] = new Parameter(typeof(Array));
                //pvh.Parameters["Random"] = new Parameter(typeof(Random));
                //pvh.Parameters["TimeZone"] = new Parameter(typeof(TimeZone));

                // Add child binding values
                for (int i = 0; i < values.Length; i++)
                {
                    IDataDescriptor value = values[i];
                    Type            type  = value.DataType;
                    if (type != null && !pvh.Parameters.Contains(type.Name))
                    {
                        pvh.Parameters[type.Name] = new Parameter(type);
                    }
                    pvh.Parameters[i.ToString()] = new Parameter(value.Value, type);
                }
                evaluator.VariableHolder = pvh;
                Tree tree = ep.Parse(expression);
                result = evaluator.Eval(tree);
                return(TypeConverter.Convert(result, targetType, out result));
            }
            catch (Exception)
            {
                return(false);
            }
        }
    /// <summary>
    /// Evaluates a simple expression, given via the parameter <paramref name="parameter"/>.
    /// </summary>
    /// <remarks>
    /// This converter will often be used in XAML files. Note that in XAML, an attribute beginning with a <c>'{'</c> character
    /// is interpreted as an invocation of a markup extension. So the expression "{0} + 5" must be escaped like this:
    /// <c>"{}{0} + 5"</c>. Note also that the boolean AND operator (<c>"&&"</c>) must be escaped too like this: <c>"{}{0} &amp;&amp; true"</c>.
    /// </remarks>
    /// <param name="values">The values used for the variables {0} .. {n}.</param>
    /// <param name="targetType">Type to that the evaluated result should be converted.</param>
    /// <param name="parameter">String containing the expression. Variables can be accessed via numbers in
    /// curly braces, for example "!({0} || {2})". The variables are mapped to the values specified by
    /// the <paramref name="values"/> array.</param>
    /// <param name="result">Will return the evaluated result of the given <paramref name="targetType"/>.</param>
    public bool Convert(IDataDescriptor[] values, Type targetType, object parameter, out object result)
    {
      result = null;
      string expression = parameter as string;
      if (string.IsNullOrEmpty(expression))
        return false;
      try
      {
        // We're using an expression parser from "devilplusplus", "C# Eval function"
        // See http://www.codeproject.com/KB/dotnet/Expr.aspx
        // The parser was slightly adapted to our needs:
        // - To access a variable, the variable identifier has to be written in curly braces, for example:
        //   {0} + {1}

        Parser ep = new Parser();
        Evaluator evaluator = new Evaluator();
        ParameterVariableHolder pvh = new ParameterVariableHolder();

        // The used expression parser supports access to static functions for those of the parameters whose type is a class.
        // We could add classes here like the code commented out below. To access a static member on the string class,
        // the expression could be for example: {string}.{Empty}
        // For now, we don't need this functionality, so we don't add types (Albert, 2009-04-22).

        //pvh.Parameters["char"] = new Parameter(typeof(char));
        //pvh.Parameters["sbyte"] = new Parameter(typeof(sbyte));
        //pvh.Parameters["byte"] = new Parameter(typeof(byte));
        //pvh.Parameters["short"] = new Parameter(typeof(short)); 
        //pvh.Parameters["ushort"] = new Parameter(typeof(ushort)); 
        //pvh.Parameters["int"] = new Parameter(typeof(int));
        //pvh.Parameters["uint"] = new Parameter(typeof(uint));
        //pvh.Parameters["long"] = new Parameter(typeof(string));
        //pvh.Parameters["ulong"] = new Parameter(typeof(ulong));
        //pvh.Parameters["float"] = new Parameter(typeof(float));
        //pvh.Parameters["double"] = new Parameter(typeof(double));
        //pvh.Parameters["decimal"] = new Parameter(typeof(decimal));
        //pvh.Parameters["DateTime"] = new Parameter(typeof(DateTime));
        //pvh.Parameters["string"] = new Parameter(typeof(string));

        //pvh.Parameters["Guid"] = new Parameter(typeof(Guid));

        //pvh.Parameters["Convert"] = new Parameter(typeof(Convert));
        //pvh.Parameters["Math"] = new Parameter(typeof(Math));
        //pvh.Parameters["Array"] = new Parameter(typeof(Array));
        //pvh.Parameters["Random"] = new Parameter(typeof(Random));
        //pvh.Parameters["TimeZone"] = new Parameter(typeof(TimeZone));

        // Add child binding values
        for (int i = 0; i < values.Length; i++)
        {
          IDataDescriptor value = values[i];
          Type type = value.DataType;
          if (type != null && !pvh.Parameters.Contains(type.Name))
            pvh.Parameters[type.Name] = new Parameter(type);
          pvh.Parameters[i.ToString()] = new Parameter(value.Value, type);
        }
        evaluator.VariableHolder = pvh;
        Tree tree = ep.Parse(expression);
        result = evaluator.Eval(tree);
        return TypeConverter.Convert(result, targetType, out result);
      }
      catch (Exception)
      {
        return false;
      }
    }
Ejemplo n.º 3
0
        public static object Evaluate(string Expression)
        {
            #region setup

            Type type = typeof(string);

            Parser ep = new Parser();

            Jyc.Expr.Evaluator evaluater = new Jyc.Expr.Evaluator();

            ParameterVariableHolder pvh = new ParameterVariableHolder();

            pvh.Parameters["char"] = new Parameter(typeof(char));
            pvh.Parameters["sbyte"] = new Parameter(typeof(sbyte));
            pvh.Parameters["byte"] = new Parameter(typeof(byte));
            pvh.Parameters["short"] = new Parameter(typeof(short));
            pvh.Parameters["ushort"] = new Parameter(typeof(ushort));
            pvh.Parameters["int"] = new Parameter(typeof(int));
            pvh.Parameters["uint"] = new Parameter(typeof(uint));
            pvh.Parameters["long"] = new Parameter(typeof(string));
            pvh.Parameters["ulong"] = new Parameter(typeof(ulong));
            pvh.Parameters["float"] = new Parameter(typeof(float));
            pvh.Parameters["double"] = new Parameter(typeof(double));
            pvh.Parameters["decimal"] = new Parameter(typeof(decimal));
            pvh.Parameters["DateTime"] = new Parameter(typeof(DateTime));
            pvh.Parameters["Int32"] = new Parameter(typeof(Int32));

            pvh.Parameters["string"] = new Parameter(typeof(string));
            pvh.Parameters["Guid"] = new Parameter(typeof(Guid));
            pvh.Parameters["HttpUtility"] = new Parameter(typeof(HttpUtility));
            pvh.Parameters["Convert"] = new Parameter(typeof(Convert));
            pvh.Parameters["Math"] = new Parameter(typeof(Math));
            pvh.Parameters["Array "] = new Parameter(typeof(Array));
            pvh.Parameters["Random"] = new Parameter(typeof(Random));
            pvh.Parameters["TimeZone"] = new Parameter(typeof(TimeZone));
            pvh.Parameters["AppDomain "] = new Parameter(typeof(AppDomain));
            pvh.Parameters["Console"] = new Parameter(typeof(Console));

            evaluater.VariableHolder = pvh;
            #endregion

            Tree tree = ep.Parse(Expression);

            object result = evaluater.Eval(tree);

            return result;
        }
Ejemplo n.º 4
0
 public object Eval(string text)
 {
     Parser ep = new Parser();
     Tree tree = ep.Parse(text);
     return Eval(tree);
 }  
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Type type = typeof(string);

            Parser ep = new Parser();

            Jyc.Expr.Evaluator evaluater = new Jyc.Expr.Evaluator();

            ParameterVariableHolder pvh = new ParameterVariableHolder();

            pvh.Parameters["char"] = new Parameter(typeof(char));
            pvh.Parameters["sbyte"] = new Parameter(typeof(sbyte));
            pvh.Parameters["byte"] = new Parameter(typeof(byte));
            pvh.Parameters["short"] = new Parameter(typeof(short));
            pvh.Parameters["ushort"] = new Parameter(typeof(ushort));
            pvh.Parameters["int"] = new Parameter(typeof(int));
            pvh.Parameters["uint"] = new Parameter(typeof(uint));
            pvh.Parameters["long"] = new Parameter(typeof(string));
            pvh.Parameters["ulong"] = new Parameter(typeof(ulong));
            pvh.Parameters["float"] = new Parameter(typeof(float));
            pvh.Parameters["double"] = new Parameter(typeof(double));
            pvh.Parameters["decimal"] = new Parameter(typeof(decimal));
            pvh.Parameters["DateTime"] = new Parameter(typeof(DateTime));

            pvh.Parameters["string"] = new Parameter(typeof(string));
            pvh.Parameters["Guid"] = new Parameter(typeof(Guid));
            pvh.Parameters["HttpUtility"] = new Parameter(typeof(HttpUtility));
            pvh.Parameters["Convert"] = new Parameter(typeof(Convert));
            pvh.Parameters["Math"] = new Parameter(typeof(Math));
            pvh.Parameters["Array "] = new Parameter(typeof(Array));
            pvh.Parameters["Random"] = new Parameter(typeof(Random));
            pvh.Parameters["TimeZone"] = new Parameter(typeof(TimeZone));
            pvh.Parameters["AppDomain "] = new Parameter(typeof(AppDomain));
            pvh.Parameters["Console"] = new Parameter(typeof(Console));
            pvh.Parameters["evaluater"] = new Parameter(evaluater);

            evaluater.VariableHolder = pvh;

            while (true)
            {
                System.Console.WriteLine("Input line,press Return to Eval:");
                string line = System.Console.ReadLine().Trim();
                if (string.IsNullOrEmpty(line))
                    break;
                try
                {
                    Tree tree = ep.Parse(line);

                    tree.Print(System.Console.Out);

                    object result = evaluater.Eval(tree);

                    if( result != null )
                        System.Console.WriteLine("Resut:{0}", result);
                    else
                        System.Console.WriteLine("Resut is null");
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("Exception:" + e.GetType().Name +"->"+ e.Message);
                }
            }
        }