码迷,mamicode.com
首页 > 其他好文 > 详细

表达式树基础代码

时间:2014-09-24 23:30:27      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   div   sp   art   cti   on   c   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace TestExpressionLambda
{
    class ProgramExpression
    {
        static void Main(string[] args)
        {
            ExpressionAdd();

            ExpressionFromlambda();

            ExpressionStartWith();

            Console.Read();
        }

        /// <summary>
        /// 最简单的加法表达式树
        /// 
        ///                   根节点
        ///                   BanaryExpression
        ///                   NodeType = Add
        ///                   Type=System.Int32
        /// 
        ///     左叶子                          右叶子
        ///     firstArg                        secondArg
        ///     ConstantExpression              ConstantExpression
        ///     NodeType=Constant               NodeType=Constant
        ///     Type=System.Int32               Type=System.Int32
        ///     Value=3                         Value=2
        /// 
        /// 
        /// </summary>
        private static void ExpressionAdd()
        {
            //构建常量表达式3
            ConstantExpression firstArg = Expression.Constant(3);
            //构建常量表达式2
            ConstantExpression secondArg = Expression.Constant(2);

            //构建+表达式 3+2
            BinaryExpression add = Expression.Add(firstArg, secondArg);
            Console.WriteLine(add);//输出 : (3+2)

            //获取委托表达式
            Expression<Func<int>> addExpression = Expression.Lambda<Func<int>>(add);
            //Compile出委托,内部调用了emit去构造il
            Func<int> addFunc = addExpression.Compile();

            Console.WriteLine(addFunc);//输出System.Func~1[Int32]
            Console.WriteLine(addFunc());//输出执行的结果:5
        }

        /// <summary>
        /// 简单从lambda创建表达式树
        /// ps:不能讲带有语句块的lambda转换成表达式树
        /// </summary>
        private static void ExpressionFromlambda()
        {
            Expression<Func<int>> lambadExpression = () => 3 + 2;
            Func<int> lambdaFunc = lambadExpression.Compile();
            Console.WriteLine(lambdaFunc);
            Console.WriteLine(lambdaFunc());
        }

        /// <summary>
        /// 表达式树去调用string类型的StartWith方法
        /// 要实现类型lambda表达式:(x,y) => x.StartsWith(y)
        /// </summary>
        private static void ExpressionStartWith()
        {
            //反射取到string的StartsWith方法
            MethodInfo method = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });

            //取参数表达式:x,y
            ParameterExpression x = Expression.Parameter(typeof(string), "x");
            ParameterExpression y = Expression.Parameter(typeof(string), "y");

            //StartsWith方法需要的参数
            Expression[] methodArgs = new[] { y };
            //生成调用表达式
            MethodCallExpression call = Expression.Call(x, method, y);

            var lambdaParameters = new[] { x, y };
            //Expression.Lambda方法生成表达式树委托
            Expression<Func<string, string, bool>> lambda = Expression.Lambda<Func<string, string, bool>>(call,
                lambdaParameters);

            //Compile出委托
            Func<string, string, bool> func = lambda.Compile();

            Console.WriteLine(func("First", "Second"));
            Console.WriteLine(func("First", "Fir"));
        }
    }
}

 

表达式树基础代码

标签:blog   io   ar   div   sp   art   cti   on   c   

原文地址:http://www.cnblogs.com/12taotie21/p/3991659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!