示例#1
0
        public void TestConditional2()
        {
            Expression <Func <TestClassA, string> > path      = a => a.B.S;
            Expression <Func <TestClassA, bool> >   condition = a => a.S == "zzz";
            Expression assign = Expression.Assign(path.Body, Expression.Constant("qxx"));
            Expression test   = new ParameterReplacer(condition.Parameters[0], path.Parameters[0]).Visit(condition.Body);
            Expression <Action <TestClassA> > exp = Expression.Lambda <Action <TestClassA> >(Expression.IfThenElse(test, assign, Expression.Default(typeof(void))), path.Parameters);
            var action = LambdaCompiler.Compile(exp, CompilerOptions.All);
            var o      = new TestClassA {
                S = "zzz"
            };

            action(o);
            Assert.IsNotNull(o.B);
            Assert.AreEqual(o.B.S, "qxx");
        }
示例#2
0
        public void TestSubLambda4()
        {
            Expression <Func <TestClassA, IEnumerable <TestClassB> > > exp = a => a.ArrayB.Where(b => b.S == a.B.C.S);

            Expression where = exp.Body;
            ParameterExpression temp       = Expression.Variable(typeof(IEnumerable <TestClassB>));
            Expression          assignTemp = Expression.Assign(temp, where);
            Expression <Func <TestClassA, string> > path = a => a.B.C.S;
            Expression left    = new ParameterReplacer(path.Parameters[0], exp.Parameters[0]).Visit(path.Body);
            Expression assignS = Expression.Assign(left, Expression.Constant("zzz"));
            Expression any     = Expression.Call(anyMethod.MakeGenericMethod(typeof(TestClassB)), temp);
            var        exp2    = Expression.Lambda <Func <TestClassA, bool> >(Expression.Block(typeof(bool), new[] { temp }, assignTemp, assignS, any), exp.Parameters);

            var f = Compile(exp2, CompilerOptions.All);

            Assert.IsTrue(f(new TestClassA {
                ArrayB = new[] { new TestClassB {
                                     S = "zzz"
                                 }, }
            }));
        }