Example #1
0
        public void ConditionalMember_Compile_Field_Ref()
        {
            var p = Expression.Parameter(typeof(Qux));
            var q = new Qux();

            var m1 = CSharpExpression.ConditionalField(p, "Y");
            var f1 = Expression.Lambda <Func <Qux, int?> >(m1, p);
            var d1 = f1.Compile();

            Assert.AreEqual(42, d1(q));
            Assert.IsNull(d1(null));

            var m2 = CSharpExpression.ConditionalField(p, "O");
            var f2 = Expression.Lambda <Func <Qux, int?> >(m2, p);
            var d2 = f2.Compile();

            Assert.AreEqual(42, d2(q));
            Assert.IsNull(d2(null));

            var m3 = CSharpExpression.ConditionalField(p, "T");
            var f3 = Expression.Lambda <Func <Qux, string> >(m3, p);
            var d3 = f3.Compile();

            Assert.AreEqual("bar", d3(q));
            Assert.IsNull(d3(null));
        }
        public void ConditionalCall_Compile_Ref()
        {
            var p = Expression.Parameter(typeof(Qux));
            var q = new Qux();

            var m1 = CSharpExpression.ConditionalCall(p, typeof(Qux).GetMethod("X"));
            var f1 = Expression.Lambda <Func <Qux, int?> >(m1, p);
            var d1 = f1.Compile();

            Assert.AreEqual(42, d1(q));
            Assert.IsNull(d1(null));

            var m2 = CSharpExpression.ConditionalCall(p, typeof(Qux).GetMethod("N"));
            var f2 = Expression.Lambda <Func <Qux, int?> >(m2, p);
            var d2 = f2.Compile();

            Assert.AreEqual(42, d2(q));
            Assert.IsNull(d2(null));

            var m3 = CSharpExpression.ConditionalCall(p, typeof(Qux).GetMethod("S"));
            var f3 = Expression.Lambda <Func <Qux, string> >(m3, p);
            var d3 = f3.Compile();

            Assert.AreEqual("bar", d3(q));
            Assert.IsNull(d3(null));

            var m4 = CSharpExpression.ConditionalCall(p, typeof(Qux).GetMethod("V"));
            var f4 = Expression.Lambda <Action <Qux> >(m4, p);
            var d4 = f4.Compile();

            d4(q); // does not throw
        }