当前位置: 首页>>代码示例>>C#>>正文


C# Expression.Evaluate方法代码示例

本文整理汇总了C#中Expression.Evaluate方法的典型用法代码示例。如果您正苦于以下问题:C# Expression.Evaluate方法的具体用法?C# Expression.Evaluate怎么用?C# Expression.Evaluate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Expression的用法示例。


在下文中一共展示了Expression.Evaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConstantFoldingDoesPreserveType

        public void ConstantFoldingDoesPreserveType()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("NonNullInt", typeof (int));
            dataTable.Columns.Add("NullInt", typeof (int));

            DataRow dataRow = dataTable.NewRow();
            dataRow["NonNullInt"] = 1;
            dataTable.Rows.Add(dataRow);
            
            DataColumnPropertyBinding[] dataRowProperties = DataRowPropertyProvider.GetProperties(dataTable);
            
            DataContext dataContext = new DataContext();
            dataContext.Constants.Add("DataRow", dataRow, dataRowProperties);

			Expression<object> exprWithNonNullInt = new Expression<object>();
            exprWithNonNullInt.DataContext = dataContext;
            exprWithNonNullInt.Text = "DataRow.NonNullInt";
            Assert.AreEqual(typeof(int), exprWithNonNullInt.Resolve());
            Assert.AreEqual(1, exprWithNonNullInt.Evaluate());

			Expression<object> exprWithNullInt = new Expression<object>();
            exprWithNullInt.DataContext = dataContext;
            exprWithNullInt.Text = "DataRow.NullInt";
            Assert.AreEqual(typeof(int), exprWithNullInt.Resolve());
            Assert.AreEqual(null, exprWithNullInt.Evaluate());
        }
开发者ID:chenzuo,项目名称:nquery,代码行数:27,代码来源:ConstantFoldingTests.cs

示例2: SimpleExpression

		public void SimpleExpression()
		{
			Expression<string> expr = new Expression<string>("'Hello' + ' ' + 'World!'");
			string result = expr.Evaluate();

			Assert.AreEqual("Hello World!", result);
		}
开发者ID:chenzuo,项目名称:nquery,代码行数:7,代码来源:ExpressionTests.cs

示例3: SimpleConversion

		public void SimpleConversion()
		{
			Expression<double> expr = new Expression<double>("2 * 100");
			double result = expr.Evaluate();

			Assert.AreEqual(200.0d, result);
		}
开发者ID:chenzuo,项目名称:nquery,代码行数:7,代码来源:ExpressionTests.cs

示例4: ConversionToLessSpecficWithBoxingToObject

		public void ConversionToLessSpecficWithBoxingToObject()
		{
			Expression<object> expr = new Expression<object>("2 + 2");
			object result = expr.Evaluate();

			Assert.AreEqual(4, result);
		}
开发者ID:chenzuo,项目名称:nquery,代码行数:7,代码来源:ExpressionTests.cs

示例5: Main

        private static void Main(string[] args)
        {
            var expr = new Expression("233");

            Console.WriteLine(expr.Evaluate());

            Console.ReadLine();
        }
开发者ID:victorprocure,项目名称:EstimatingExpressionEvaluator,代码行数:8,代码来源:Program.cs

示例6: OmittingOptionalAtWorksInExpression

		public void OmittingOptionalAtWorksInExpression()
		{
			Expression<object> expression = new Expression<object>("MyParam1 + ' + ' + @MyParam2");
			expression.Parameters.Add("@MyParam1", typeof (string));
			expression.Parameters.Add("MyParam2", typeof (string));
			expression.Parameters["MyParam1"].Value = "MyParam1Value";
			expression.Parameters["@MyParam2"].Value = "MyParam2Value";
			object value = expression.Evaluate();
			Assert.AreEqual(value, "MyParam1Value + MyParam2Value");
		}
开发者ID:chenzuo,项目名称:nquery,代码行数:10,代码来源:ParameterTests.cs

示例7: Arithmetics

        public void Arithmetics(string formula)
        {
            var expression = new Expression(formula);
            var lambda = expression.ToLambda<object>();

            var m1 = Measure(() => expression.Evaluate());
            var m2 = Measure(() => lambda());

            PrintResult(formula, m1, m2);
        }
开发者ID:sklose,项目名称:NCalc2,代码行数:10,代码来源:Performance.cs

示例8: ConversionToLessSpecificType2

		public void ConversionToLessSpecificType2()
		{
			Derived derived = new Derived();

			Expression<Base> expr = new Expression<Base>("Test");
			expr.Parameters.Add("Test", typeof(Base), derived);
			Base result = expr.Evaluate();

			Assert.AreSame(derived, result);
		}
开发者ID:chenzuo,项目名称:nquery,代码行数:10,代码来源:ExpressionTests.cs

示例9: ParameterAccess

        public void ParameterAccess(string formula)
        {
            var expression = new Expression(formula);
            var lambda = expression.ToLambda<Context, int>();

            var context = new Context {Param1 = 4, Param2 = 9};
            expression.Parameters["Param1"] = 4;
            expression.Parameters["Param2"] = 9;

            var m1 = Measure(() => expression.Evaluate());
            var m2 = Measure(() => lambda(context));

            PrintResult(formula, m1, m2);
        }
开发者ID:sklose,项目名称:NCalc2,代码行数:14,代码来源:Performance.cs

示例10: ParameterScenario

		private static void ParameterScenario()
		{
			#region Parameter Scenario

			const int numberOfRuns = 10000;

			Console.WriteLine("Using constants and parameters in scenario where the value frequently changes.");
			Console.WriteLine("Executing scenario {0:N0} times.", numberOfRuns);

			int numberOfCompilations = 0;
			Stopwatch stopwatch;

			// Testing with constants

			Console.WriteLine("Constants");
			Expression<string> expressionUsingConstant = new Expression<string>();
			expressionUsingConstant.Text = "TO_STRING(MagicValue * 42 / 1000.0) + ' the result'";
			expressionUsingConstant.CompilationSucceeded += delegate { numberOfCompilations++; };

			numberOfCompilations = 0;
			stopwatch = Stopwatch.StartNew();
			for (int i = 0; i < numberOfRuns; i++)
			{
				expressionUsingConstant.DataContext.Constants.Add("MagicValue", i);
				expressionUsingConstant.Evaluate();
				expressionUsingConstant.DataContext.Constants.Remove("MagicValue");
			}
			Console.WriteLine("\tNeeded Time  : {0:N0} msecs", stopwatch.ElapsedMilliseconds);
			Console.WriteLine("\tCompilations : {0:N0}", numberOfCompilations);

			// Testing with parameters

			Console.WriteLine("Parameters");
			Expression<string> expressionUsingParameters = new Expression<string>();
			expressionUsingParameters.Parameters.Add("MagicValue", typeof(int));
			expressionUsingParameters.Text = "TO_STRING(MagicValue * 42 / 1000.0) + ' the result'";
			expressionUsingParameters.CompilationSucceeded += delegate { numberOfCompilations++; };

			numberOfCompilations = 0;
			stopwatch = Stopwatch.StartNew();
			for (int i = 0; i < numberOfRuns; i++)
			{
				expressionUsingParameters.Parameters["MagicValue"].Value = i;
				expressionUsingParameters.Evaluate();
			}
			Console.WriteLine("\tNeeded Time  : {0:N0} msecs", stopwatch.ElapsedMilliseconds);
			Console.WriteLine("\tCompilations : {0:N0}", numberOfCompilations);

			#endregion
		}
开发者ID:chenzuo,项目名称:nquery,代码行数:50,代码来源:Program.cs

示例11: CustomParametersShouldReturnNull

        public void CustomParametersShouldReturnNull()
        {
            var e = new Expression("x");

            e.EvaluateParameter += delegate(string name, ParameterArgs args)
            {
                Assert.IsFalse(args.HasResult);
                if (name == "x")
                    args.Result = null;
                Assert.IsTrue(args.HasResult);
            };

            Assert.AreEqual(null, e.Evaluate());
        }
开发者ID:se5a,项目名称:NCalc-Edge,代码行数:14,代码来源:Fixtures.cs

示例12: CustomFunctionShouldReturnNull

        public void CustomFunctionShouldReturnNull()
        {
            var e = new Expression("SecretOperation(3, 6)");

            e.EvaluateFunction += delegate(string name, FunctionArgs args)
            {
                Assert.IsFalse(args.HasResult);
                if (name == "SecretOperation")
                    args.Result = null;
                Assert.IsTrue(args.HasResult);
            };

            Assert.AreEqual(null, e.Evaluate());
        }
开发者ID:se5a,项目名称:NCalc-Edge,代码行数:14,代码来源:Fixtures.cs

示例13: Add

        public void Add(Expression e)
        {
            // handle enumerables
            var ienum = e as IEnumerable;
            if (ienum != null)
            {
                foreach (var value in ienum)
                {
                    AddValue(value);
                }
                return;
            }

            // handle expressions
            AddValue(e.Evaluate());
        }
开发者ID:mdjabirov,项目名称:C1Decompiled,代码行数:16,代码来源:Tally.cs

示例14: DynamicParameterAccess

        public void DynamicParameterAccess(string formula)
        {
            var expression = new Expression(formula);
            var lambda = expression.ToLambda<Context, int>();

            var context = new Context { Param1 = 4, Param2 = 9 };
            expression.EvaluateParameter += (name, args) =>
            {
                if (name == "Param1") args.Result = context.Param1;
                if (name == "Param2") args.Result = context.Param2;
            };

            var m1 = Measure(() => expression.Evaluate());
            var m2 = Measure(() => lambda(context));

            PrintResult(formula, m1, m2);
        }
开发者ID:sklose,项目名称:NCalc2,代码行数:17,代码来源:Performance.cs

示例15: EvaluateWhere

        private Expression EvaluateWhere(EvalStore store, Expression a, Expression b)
        {
            if (a.ExpressionType != ExpressionType.CONST) a = a.Evaluate(store);
            if (a.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty;

            if (b.ExpressionType != ExpressionType.CONST) b = b.Evaluate(store);
            if (b.Value.DataType == ElementType.EMPTY) return ConstExpr.Empty;

            if (a.Value.DataType == ElementType.TABLE && b.Value.DataType == ElementType.TABLE) {
                Table r = (Table)a.Value;
                Table s = (Table)b.Value;
                r.IntersectWith(s);
                return new ConstExpr(r);
            }
            else {
                throw new Exception("Incompatible operands: " + a.Value.DataType + "," + b.Value.DataType);
            }
        }
开发者ID:sekcheong,项目名称:parser,代码行数:18,代码来源:AndOperator.cs


注:本文中的Expression.Evaluate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。