public void TestItInternalInternal <T, TRet>(Expression <T> expr, Func <T, TRet> howToCall, ISimpleSerializer serializer) { var expr1 = EditableExpression.Create(Funcletizer.PartialEval(expr)); byte[] data = serializer.Serialize(expr1); EditableExpression expr2 = (EditableExpression)serializer.Deserialize(data); T func2 = ((Expression <T>)expr2.ToExpression()).Compile(); var value1 = howToCall(func2); var value2 = howToCall(expr.Compile()); Assert.AreEqual(value2, value1); }
public bool TestExpression(EditableExpression expr, int value) { return(((Expression <Predicate <int> >)expr.ToExpression()).Compile()(value)); }
private static void InvokeExpression <T1, T2>(EditableExpression mutableLambda, T1 value1, T2 value2) { LambdaExpression e = mutableLambda.ToExpression() as LambdaExpression; Console.WriteLine(e.ToString() + " = " + e.Compile().DynamicInvoke(value1, value2)); }
private static void InvokeExpression(EditableExpression mutableLambda) { LambdaExpression e = mutableLambda.ToExpression() as LambdaExpression; Console.WriteLine(e.ToString() + " = " + e.Compile().DynamicInvoke()); }
/// <summary> /// Evaluates this instance. (at the moment, only two explicit lamda forms are supported) /// </summary> /// <returns> </returns> bool IOperationEvaluator.Evaluate( ) { if (Expression != null) { EditableExpression expression = Expression; LambdaExpression delegateExpression = ( LambdaExpression )expression.ToExpression( ); object compiledDelegate = delegateExpression.Compile( ); if (compiledDelegate is MulticastDelegate) { MulticastDelegate multicastDelegate = ( MulticastDelegate )compiledDelegate; compiledDelegate = multicastDelegate.DynamicInvoke(null); } if (compiledDelegate is Func <bool> ) { Func <bool> delegateMethod = (Func <bool>)compiledDelegate; return(delegateMethod( )); } if (compiledDelegate is Action <IExpressionEvaluator> ) { Action <IExpressionEvaluator> evaluatorDelegate = (Action <IExpressionEvaluator>)compiledDelegate; evaluatorDelegate(this); } } bool?evaluation = null; foreach (ExpressionContainer container in _conditionChains) { if (container.Type == ExpressionType.Evaluate) { evaluation = container.Evaluate( ); } if (container.Type == ExpressionType.And) { evaluation = evaluation.GetValueOrDefault( ) && container.Evaluate( ); } if (container.Type == ExpressionType.Or) { evaluation = evaluation.GetValueOrDefault( ) || container.Evaluate( ); } if (container.Type == ExpressionType.Is) { evaluation = container.Evaluate( ); } if (container.Type == ExpressionType.Not) { evaluation = !container.Evaluate( ); } if (container.Type == ExpressionType.Custom) { evaluation = container.Evaluate( ); } } return(evaluation.GetValueOrDefault( )); }