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


C# Expression.ToLambda方法代码示例

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


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

示例1: FunctionWithDynamicParameterAccess

        public void FunctionWithDynamicParameterAccess(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;
            };
            expression.EvaluateFunction += (name, args) =>
            {
                if (name == "Foo")
                {
                    var param = args.EvaluateParameters();
                    args.Result = context.Foo((int) param[0], (int) param[1]);
                }
            };

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

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

示例2: ShouldHandleIntegers

        public void ShouldHandleIntegers(string input, int expected)
        {
            var expression = new Expression(input);
            var sut = expression.ToLambda<int>();

            Assert.Equal(sut(), expected);
        }
开发者ID:sklose,项目名称:NCalc2,代码行数:7,代码来源:Lambdas.cs

示例3: ShouldHandleDataConversions

        public void ShouldHandleDataConversions(string input, bool expected)
        {
            var expression = new Expression(input);
            var sut = expression.ToLambda<Context, bool>();
            var context = new Context { FieldA = 7, FieldB = "test", FieldC = 2.4m, FieldE = 2 };

            Assert.Equal(expected, sut(context));
        }
开发者ID:sklose,项目名称:NCalc2,代码行数:8,代码来源:Lambdas.cs

示例4: ShouldHandleTernaryOperator

        public void ShouldHandleTernaryOperator()
        {
            var expression = new Expression("Test(1, 2) = 3 ? 1 : 2");
            var sut = expression.ToLambda<Context, int>();
            var context = new Context();

            Assert.Equal(sut(context), 1);
        }
开发者ID:sklose,项目名称:NCalc2,代码行数:8,代码来源:Lambdas.cs

示例5: ShouldHandleCustomFunctions

        public void ShouldHandleCustomFunctions()
        {
            var expression = new Expression("Test(Test(1, 2), 3)");
            var sut = expression.ToLambda<Context, int>();
            var context = new Context();

            Assert.Equal(sut(context), 6);
        }
开发者ID:sklose,项目名称:NCalc2,代码行数:8,代码来源:Lambdas.cs

示例6: ShouldHandleParameters

        public void ShouldHandleParameters()
        {
            var expression = new Expression("[FieldA] > 5 && [FieldB] = 'test'");
            var sut = expression.ToLambda<Context, bool>();
            var context = new Context {FieldA = 7, FieldB = "test"};

            Assert.True(sut(context));
        }
开发者ID:sklose,项目名称:NCalc2,代码行数:8,代码来源:Lambdas.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: 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

示例9: 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

示例10: ShouldHandleBuiltInFunctions

 public void ShouldHandleBuiltInFunctions(string input)
 {
     var expression = new Expression(input);
     var sut = expression.ToLambda<bool>();
     Assert.True(sut());
 }
开发者ID:sklose,项目名称:NCalc2,代码行数:6,代码来源:Lambdas.cs


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