public void TestBadTypeReplacement()
        {
            var cc = new CodeContext();

            cc.Add("d", Expression.Variable(typeof(int), "f"));
            cc.Add("f", Expression.Constant(20.0));

            var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(Expression.Variable(typeof(int), "d"), cc);
        }
        public void TestLambda()
        {
            Expression <Func <testLambdaSimple, float> > lambdaExpr = t => t.pt / ((float)100.0);

            CodeContext cc   = new CodeContext();
            var         expr = ParameterReplacementExpressionVisitor.ReplaceParameters(lambdaExpr, cc);

            Assert.AreEqual(lambdaExpr.ToString(), expr.ToString(), "lambda changed");
        }
        /// <summary>
        /// See if this sequence type is actually a normal array or sub query expression hidden by an anonymous array index or similar.
        /// </summary>
        /// <param name="expr"></param>
        /// <returns></returns>
        private static Expression AttemptTranslationToArray(Expression expr, ICodeContext cc)
        {
            var preplacements = ParameterReplacementExpressionVisitor.ReplaceParameters(expr, cc);
            var r             = TranslatingExpressionVisitor.Translate(preplacements, cc.CacheCookies, e => e);

            if (r == preplacements)
            {
                return(null);
            }
            return(r);
        }
        public void TestNoReplacement()
        {
            var cc    = new CodeContext();
            var myvar = Expression.Variable(typeof(int), "d");
            var expr  = ParameterReplacementExpressionVisitor.ReplaceParameters(myvar, cc);

            var asp = expr as ParameterExpression;

            Assert.IsNotNull(asp, "expected the returned expression to be correct");
            Assert.AreEqual(typeof(int), asp.Type, "bad type coming back");
            Assert.AreEqual("d", asp.Name, "variable name");
        }
        public void TestDeclarableParameterReplacement()
        {
            var e1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var e2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));

            var cc = new CodeContext();

            cc.Add(e1.ParameterName, e2);

            var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(e1, cc);

            Assert.AreEqual(e2, expr, "value of translation");
        }
        public void TestSimpleReplacement()
        {
            var cc = new CodeContext();

            cc.Add("d", Expression.Constant(20));

            var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(Expression.Variable(typeof(int), "d"), cc);

            var asconst = expr as ConstantExpression;

            Assert.IsNotNull(asconst, "constant replacement");
            Assert.AreEqual(20, asconst.Value, "value of translation");
        }
        public void TestLambdaWithPredefinedNameAsParam()
        {
            /// This is basically a scope test! :-)
            Expression <Func <testLambdaSimple, float> > lambdaExpr = t => t.pt / ((float)100.0);

            CodeContext cc = new CodeContext();

            cc.Add("t", Expression.Parameter(typeof(testLambdaSimple), "f**k"));
            var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(lambdaExpr, cc);

            Assert.AreEqual(lambdaExpr.ToString(), expr.ToString(), "lambda changed");
            Assert.AreEqual("f**k", (cc.GetReplacement("t") as ParameterExpression).Name, "code context was altered");
        }
        public void TestSubqueryPatternReplacement()
        {
            var qexpr = new QuerySourceReferenceExpression(new dummyQuerySource());

            CodeContext cc = new CodeContext();

            cc.Add("d", Expression.Variable(typeof(int), "f"));
            cc.Add(qexpr.ReferencedQuerySource, Expression.Parameter(typeof(int), "d"));

            var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(qexpr, cc);

            var asp = expr as ParameterExpression;

            Assert.IsNotNull(asp, "expected the returned expression to be of type ParameterExpression");
            Assert.AreEqual(typeof(int), asp.Type, "bad type coming back");
            Assert.AreEqual("f", asp.Name, "variable name");
        }
        public void TestArrayReplacement()
        {
            var myvar = Expression.Variable(typeof(int[]), "d");
            var myref = Expression.ArrayIndex(myvar, Expression.Constant(1));

            var cc = new CodeContext();

            cc.Add("d", Expression.Variable(typeof(int[]), "dude"));

            var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(myref, cc);

            var be = expr as BinaryExpression;

            Assert.AreEqual(ExpressionType.ArrayIndex, be.NodeType, "index bad");
            var pe = be.Left as ParameterExpression;

            Assert.IsNotNull(pe, "bad array value");
            Assert.AreEqual("dude", pe.Name, "array parameter name");
        }
        public void TestArrayIndexReplacement()
        {
            var myarray = Expression.Variable(typeof(int[]), "darray");
            var myindex = Expression.Variable(typeof(int), "d");
            var myref   = Expression.ArrayIndex(myarray, myindex);

            var cc = new CodeContext();

            cc.Add("d", Expression.Constant(10));

            var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(myref, cc);

            var be = expr as BinaryExpression;

            Assert.AreEqual(ExpressionType.ArrayIndex, be.NodeType, "index bad");
            var ce = be.Right as ConstantExpression;

            Assert.IsNotNull(ce, "reference to constant failed");
            Assert.AreEqual(10, ce.Value, "value of array index");
        }
        public IArrayInfo GetIArrayInfo(Expression expr, IGeneratedQueryCode gc, ICodeContext cc, CompositionContainer container, Func <Expression, IArrayInfo> ReGetIArrayInfo)
        {
            if (!(expr is QuerySourceReferenceExpression))
            {
                return(null);
            }

            var preplacements = ParameterReplacementExpressionVisitor.ReplaceParameters(expr, cc);
            var r             = TranslatingExpressionVisitor.Translate(preplacements, cc.CacheCookies, e => e);

            //
            // If we don't know what we are doing here, bail!
            //

            if (r == expr)
            {
                return(null);
            }

            return(ReGetIArrayInfo(r));
        }
Esempio n. 12
0
            /// <summary>
            /// Visit an expression. For each expression first make sure that it has been
            /// translated and parameter replaced. We do this recusively...
            /// </summary>
            /// <param name="expression"></param>
            /// <returns></returns>
            public override Expression Visit(Expression expression)
            {
                //
                // Sometimes we are called with a null - we ignore that! :-)
                //

                if (expression == null)
                {
                    return(null);
                }

                ///
                /// See if there are any parameter replacements that can be done out-of-band
                ///

                var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(expression, CodeContext);

                //
                // Next, attempt to translate the expr (if needed). This deals with moving from
                // one pre-done space to another.
                //

                string oldExpr = "";

                while (expr.ToString() != oldExpr)
                {
                    oldExpr = expr.ToString();
                    expr    = TranslatingExpressionVisitor.Translate(expr, CodeContext.CacheCookies, e => e.Resolve(GeneratedCode, CodeContext, MEFContainer));
                }

                //
                // Now do the rest of the parsing.
                //

                return(base.Visit(expr));
            }
Esempio n. 13
0
        /// <summary>
        /// Return a new expression with the parameters replaced.
        /// </summary>
        /// <param name="expr"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static Expression ReplaceParameters(Expression expr, ICodeContext context)
        {
            var prep = new ParameterReplacementExpressionVisitor(context);

            return(prep.Visit(expr));
        }