本文整理汇总了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);
}
示例2: ShouldHandleIntegers
public void ShouldHandleIntegers(string input, int expected)
{
var expression = new Expression(input);
var sut = expression.ToLambda<int>();
Assert.Equal(sut(), expected);
}
示例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));
}
示例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);
}
示例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);
}
示例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));
}
示例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);
}
示例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);
}
示例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);
}
示例10: ShouldHandleBuiltInFunctions
public void ShouldHandleBuiltInFunctions(string input)
{
var expression = new Expression(input);
var sut = expression.ToLambda<bool>();
Assert.True(sut());
}