public CSharpLamdaExpression GetLamdaExpression()
        {
            var interpreter = GetExpression(method.Block);

            var expression = interpreter;

            ParameterExpression[] parameters;

            if (method.IsStatic)
            {
                parameters = parameterExpressions;
            }
            else
            {
                var list = parameterExpressions.ToList();
                list.Insert(0, thisExpression);
                parameters = list.ToArray();
            }

            if (method.IsAsync)
            {
                return(new CSharpLamdaExpression(CSharpExpression.AsyncLambda(expression, parameters)));
            }

            return(new CSharpLamdaExpression(Expression.Lambda(expression, parameters)));
        }
Ejemplo n.º 2
0
        public void AsyncLambda_Factory_CanQuote()
        {
            var e   = Expression.Lambda <Func <int> >(Expression.Constant(42));
            var res = CSharpExpression.AsyncLambda <Func <Task <Expression <Func <int> > > > >(e);

            Assert.AreEqual(ExpressionType.Quote, res.Body.NodeType);
            Assert.AreSame(e, ((UnaryExpression)res.Body).Operand);
        }
Ejemplo n.º 3
0
        public void AsyncLambda_Factory_ArgumentChecking_CompatibleSignature()
        {
            var p = Expression.Parameter(typeof(int));

            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <Action>(Expression.Empty(), p));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <Action <int> >(Expression.Empty()));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <Action <string> >(Expression.Empty(), p));
        }
Ejemplo n.º 4
0
        public void AsyncLambda_Visitor()
        {
            var res = CSharpExpression.AsyncLambda(Expression.Empty());

            var v = new V();

            Assert.AreSame(res, v.Visit(res));
            Assert.IsTrue(v.Visited);
        }
Ejemplo n.º 5
0
        public void DynamicAwait_Compile()
        {
            var e    = Expression.Constant(Task.FromResult(42));
            var expr = DynamicCSharpExpression.DynamicAwait(e);

            var f = CSharpExpression.AsyncLambda <Func <Task <object> > >(expr);

            Assert.AreEqual(42, f.Compile()().Result);
        }
Ejemplo n.º 6
0
        public void AsyncLambda_Factory_ArgumentChecking_MustHaveDelegateType()
        {
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <int>(Expression.Empty()));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <MulticastDelegate>(Expression.Empty()));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <Delegate>(Expression.Empty()));

            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(typeof(int), Expression.Empty()));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(typeof(MulticastDelegate), Expression.Empty()));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(typeof(Delegate), Expression.Empty()));
        }
Ejemplo n.º 7
0
        public void AsyncLambda_Properties()
        {
            var body       = Expression.Constant(42);
            var parameters = new[] { Expression.Parameter(typeof(int)) };
            var res        = CSharpExpression.AsyncLambda(body, parameters);

            Assert.AreEqual(CSharpExpressionType.AsyncLambda, res.CSharpNodeType);
            Assert.AreSame(body, res.Body);
            Assert.IsTrue(parameters.SequenceEqual(res.Parameters));
            Assert.AreEqual(typeof(Func <int, Task <int> >), res.Type);
        }
Ejemplo n.º 8
0
        public void ShallowVisitor_Basics()
        {
            var v = new V();

            var c = Expression.Constant(42);
            var f = Expression.Lambda(c);
            var a = CSharpExpression.AsyncLambda(c);

            Assert.AreNotSame(c, v.Visit(c));
            Assert.AreSame(f, v.Visit(f));
            Assert.AreSame(a, v.Visit(a));
        }
Ejemplo n.º 9
0
        public void AsyncLambda_Factory_ArgumentChecking_NoRefParameters()
        {
            var p = Expression.Parameter(typeof(int).MakeByRefType());

            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(Expression.Empty(), p));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(Expression.Empty(), new[] { p }.AsEnumerable()));

            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(typeof(ByRef), Expression.Empty(), p));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(typeof(ByRef), Expression.Empty(), new[] { p }.AsEnumerable()));

            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <ByRef>(Expression.Empty(), p));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <ByRef>(Expression.Empty(), new[] { p }.AsEnumerable()));
        }
Ejemplo n.º 10
0
        public void AsyncLambda_Factory_ArgumentChecking_BodyNotNull()
        {
            var p = Expression.Parameter(typeof(int));

            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.AsyncLambda(default(Expression), p));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.AsyncLambda(default(Expression), new[] { p }.AsEnumerable()));

            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.AsyncLambda(typeof(Action <int>), default(Expression), p));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.AsyncLambda(typeof(Action <int>), default(Expression), new[] { p }.AsEnumerable()));

            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.AsyncLambda <Action <int> >(default(Expression), p));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.AsyncLambda <Action <int> >(default(Expression), new[] { p }.AsEnumerable()));
        }
Ejemplo n.º 11
0
        public void AsyncLambda_Factory_ArgumentChecking_NoDuplicateParameters()
        {
            var p = Expression.Parameter(typeof(int));

            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(Expression.Empty(), p, p));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(Expression.Empty(), new[] { p, p }.AsEnumerable()));

            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(typeof(Action <int, int>), Expression.Empty(), p, p));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda(typeof(Action <int, int>), Expression.Empty(), new[] { p, p }.AsEnumerable()));

            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <Action <int, int> >(Expression.Empty(), p, p));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <Action <int, int> >(Expression.Empty(), new[] { p, p }.AsEnumerable()));
        }
        public Expression GetExpression()
        {
            var parameters = lamdaStatement.Parameters.Select(x =>
                                                              Expression.Parameter(x.Type, x.Name)).ToArray();

            parameterExpressions.AddRange(parameters);
            var body = expressionInterpreterHandler.GetExpression(lamdaStatement.Body);

            parameterExpressions.RemoveAll(x => parameters.Any(y => y == x));

            var type         = (Type)lamdaStatement.Type;
            var delegateType = type;

            if (!type.IsSubclassOf(typeof(MulticastDelegate)))
            {
                delegateType = type.GenericTypeArguments.FirstOrDefault();
                MethodInfo lambdaMethod;
                if (lamdaStatement.IsAsync)
                {
                    lambdaMethod = typeof(CSharpExpression).GetMethod("AsyncLambda", new Type[] { typeof(Type), typeof(Expression), typeof(ParameterExpression[]) });
                }
                else
                {
                    lambdaMethod = typeof(Expression).GetMethod("Lambda", new Type[] { typeof(Type), typeof(Expression), typeof(ParameterExpression[]) });
                }

                return(Expression.Call(null, lambdaMethod, Expression.Constant(delegateType), Expression.Constant(body), Expression.Constant(parameters)));
            }

            Expression lamda;

            if (lamdaStatement.IsAsync)
            {
                lamda = CSharpExpression.AsyncLambda(delegateType, body, parameters);
            }
            else
            {
                lamda = Expression.Lambda(delegateType, body, parameters);
            }


            return(lamda);
        }
Ejemplo n.º 13
0
        public void AsyncLambda_Update()
        {
            var body       = Expression.Constant(42);
            var parameters = new[] { Expression.Parameter(typeof(int)) };
            var res        = CSharpExpression.AsyncLambda <Func <int, Task <int> > >(body, parameters);

            Assert.AreSame(res, res.Update(res.Body, res.Parameters));

            var newBody       = Expression.Constant(42);
            var newParameters = new[] { Expression.Parameter(typeof(int)) };

            var upd1 = res.Update(newBody, res.Parameters);

            Assert.AreSame(newBody, upd1.Body);
            Assert.AreSame(res.Parameters, upd1.Parameters);

            var upd2 = res.Update(res.Body, newParameters);

            Assert.AreSame(res.Body, upd2.Body);
            Assert.IsTrue(newParameters.SequenceEqual(upd2.Parameters));
        }
Ejemplo n.º 14
0
        public void AsyncLambda_Factory_InferDelegateType()
        {
            var e1 = CSharpExpression.AsyncLambda(Expression.Empty());

            Assert.AreEqual(e1.Type, typeof(Func <Task>));
            Assert.IsInstanceOfType(e1, typeof(AsyncCSharpExpression <Func <Task> >));

            var e2 = CSharpExpression.AsyncLambda(Expression.Default(typeof(int)));

            Assert.AreEqual(e2.Type, typeof(Func <Task <int> >));
            Assert.IsInstanceOfType(e2, typeof(AsyncCSharpExpression <Func <Task <int> > >));

            var p = Expression.Parameter(typeof(string));

            var e3 = CSharpExpression.AsyncLambda(Expression.Empty(), p);

            Assert.AreEqual(e3.Type, typeof(Func <string, Task>));
            Assert.IsInstanceOfType(e3, typeof(AsyncCSharpExpression <Func <string, Task> >));

            var e4 = CSharpExpression.AsyncLambda(Expression.Default(typeof(int)), p);

            Assert.AreEqual(e4.Type, typeof(Func <string, Task <int> >));
            Assert.IsInstanceOfType(e4, typeof(AsyncCSharpExpression <Func <string, Task <int> > >));
        }
Ejemplo n.º 15
0
 public void AsyncLambda_Factory_ArgumentChecking_CompatibleReturnType()
 {
     AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <Func <Task <int> > >(Expression.Empty()));
     AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <Func <Task <long> > >(Expression.Constant(1)));
     AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <Func <Task <string> > >(Expression.Constant(1)));
 }
Ejemplo n.º 16
0
 public void AsyncLambda_Factory_ArgumentChecking_MustHaveAsyncReturnType()
 {
     AssertEx.Throws <ArgumentException>(() => CSharpExpression.AsyncLambda <Func <int> >(Expression.Empty()));
 }
Ejemplo n.º 17
0
        public void AsyncLambda_Factory_Covariance()
        {
            var res = CSharpExpression.AsyncLambda <Func <Task <object> > >(Expression.Constant("bar"));

            Assert.AreEqual("bar", res.Compile()().Result);
        }