Example #1
0
        public void ConditionalIndex_Compile_Ref()
        {
            var px = Expression.Parameter(typeof(QuxX));
            var qx = new QuxX();
            var ix = typeof(QuxX).GetProperty("Item");
            var ax = CSharpExpression.Bind(ix.GetIndexParameters()[0], Expression.Constant(0));
            var mx = CSharpExpression.ConditionalIndex(px, ix, ax);
            var fx = Expression.Lambda <Func <QuxX, int?> >(mx, px);
            var dx = fx.Compile();

            Assert.AreEqual(42, dx(qx));
            Assert.IsNull(dx(null));

            var pn = Expression.Parameter(typeof(QuxN));
            var qn = new QuxN();
            var jn = typeof(QuxN).GetProperty("Item");
            var an = CSharpExpression.Bind(jn.GetIndexParameters()[0], Expression.Constant(0));
            var mn = CSharpExpression.ConditionalIndex(pn, jn, an);
            var fn = Expression.Lambda <Func <QuxN, int?> >(mn, pn);
            var dn = fn.Compile();

            Assert.AreEqual(42, dn(qn));
            Assert.IsNull(dn(null));

            var ps = Expression.Parameter(typeof(QuxS));
            var qs = new QuxS();
            var js = typeof(QuxS).GetProperty("Item");
            var bs = CSharpExpression.Bind(js.GetIndexParameters()[0], Expression.Constant(0));
            var ms = CSharpExpression.ConditionalIndex(ps, js, bs);
            var fs = Expression.Lambda <Func <QuxS, string> >(ms, ps);
            var ds = fs.Compile();

            Assert.AreEqual("bar", ds(qs));
            Assert.IsNull(ds(null));
        }
        public void ConditionalIndex_Compile_Ref()
        {
            var px = Expression.Parameter(typeof(QuxX));
            var qx = new QuxX();
            var ix = typeof(QuxX).GetProperty("Item");
            var ax = CSharpExpression.Bind(ix.GetIndexParameters()[0], Expression.Constant(0));
            var mx = CSharpExpression.ConditionalIndex(px, ix, ax);
            var fx = Expression.Lambda<Func<QuxX, int?>>(mx, px);
            var dx = fx.Compile();
            Assert.AreEqual(42, dx(qx));
            Assert.IsNull(dx(null));

            var pn = Expression.Parameter(typeof(QuxN));
            var qn = new QuxN();
            var jn = typeof(QuxN).GetProperty("Item");
            var an = CSharpExpression.Bind(jn.GetIndexParameters()[0], Expression.Constant(0));
            var mn = CSharpExpression.ConditionalIndex(pn, jn, an);
            var fn = Expression.Lambda<Func<QuxN, int?>>(mn, pn);
            var dn = fn.Compile();
            Assert.AreEqual(42, dn(qn));
            Assert.IsNull(dn(null));

            var ps = Expression.Parameter(typeof(QuxS));
            var qs = new QuxS();
            var js = typeof(QuxS).GetProperty("Item");
            var bs = CSharpExpression.Bind(js.GetIndexParameters()[0], Expression.Constant(0));
            var ms = CSharpExpression.ConditionalIndex(ps, js, bs);
            var fs = Expression.Lambda<Func<QuxS, string>>(ms, ps);
            var ds = fs.Compile();
            Assert.AreEqual("bar", ds(qs));
            Assert.IsNull(ds(null));
        }