读Expression Tree上手指南(一)做习题答案

读完脑袋大哥的文章Expression Tree上手指南(一)

做习题

-a

a + b * 2

Math.Sin(x) + Math.Cos(y)

new StringBuilder(“Hello”)

new int[] { a, b, a + b}

a[i – 1] * i

a.Length > b | b >= 0

new System.Windows.Point() { X = Math.Sin(a), Y = Math.Cos(a) }

 

 class Program
    {
        static void Main(string[] args)
        {

            Method1();
            Method2();
            Method3();
            Method4();
            Method5();
            Method6();
            Method7();
            Method8();
            Console.ReadLine();


        }
        public static void Method1()
        {
            //-a
            ParameterExpression expA = Expression.Parameter(typeof(int), "a"); //参数a
            var express = Expression.Negate(expA);
            Console.WriteLine(express);
        }
        public static void Method2()
        {
            //a+b*2
            ParameterExpression expA = Expression.Parameter(typeof(int), "a"); //参数a
            ParameterExpression expB = Expression.Parameter(typeof(int), "b"); //参数a
            var express = Expression.Add(Expression.Multiply(expB, Expression.Constant(2)), expA);
            Console.WriteLine(express);
        }
        public static void Method3()
        {
            //Math.Sin(x) + Math.Cos(y)

            ParameterExpression expX = Expression.Parameter(typeof(double), "x"); //参数x
            ParameterExpression expY = Expression.Parameter(typeof(double), "y"); //参数y
            var x = Expression.Call(null, typeof(Math).GetMethod("Sin", BindingFlags.Static | BindingFlags.Public), expX);

            var y = Expression.Call(null, typeof(Math).GetMethod("Cos", BindingFlags.Static | BindingFlags.Public), expY);
            Console.WriteLine(Expression.Add(x, y)); ;
        }
        public static void Method4()
        {
            var d = Expression.Constant("abc", typeof(string));
            Type[] types = new Type[1];
            types[0] = typeof(string);
            Console.WriteLine(Expression.New(typeof(StringBuilder).GetConstructor(types), d));
        }
        public static void Method5()
        {
            //new int[] { a, b, a + b}

            List<System.Linq.Expressions.Expression> trees = new List<System.Linq.Expressions.Expression>
        { 
                Expression.Parameter(typeof(int), "a"),
                Expression.Parameter(typeof(int), "b"),
                Expression.Add(Expression.Parameter(typeof(int), "a"),
                Expression.Parameter(typeof(int), "b"))
        };
            Console.WriteLine(Expression.NewArrayInit(typeof(int), trees));
        }
        public static void Method6()
        {
            //a[i – 1] * i
            var i = Expression.Parameter(typeof(int), "i");
            var a = Expression.Parameter(typeof(int[]), "a");
            var express = Expression.Multiply(Expression.ArrayIndex(a, Expression.Subtract(i, Expression.Constant(1))), i);
            Console.WriteLine(express);
        }
        public static void Method7()
        {
            //a.Length > b | b >= 0
            var a = Expression.Parameter(typeof(int[]), "a");
            var b = Expression.Parameter(typeof(int), "b");
           Console.WriteLine(Expression.Or(Expression.GreaterThan(Expression.ArrayLength(a), b),
               Expression.GreaterThanOrEqual(b, Expression.Constant(0))));
        }
        public static void Method8()
        {
            //new System.Windows.Point() { X = Math.Sin(a), Y = Math.Cos(a) }
            ParameterExpression a = Expression.Parameter(typeof(double), "a");
            var express =
                                  Expression.MemberInit(Expression.New(typeof(System.Windows.Point)),
                                      new MemberBinding[] { Expression.Bind(typeof(System.Windows.Point).GetProperty("X"),
                                          Expression.Call(null, typeof(Math).GetMethod("Sin"), a)), 
                                                Expression.Bind(typeof(System.Windows.Point).GetProperty("Y"), Expression.Call(null, typeof(Math).GetMethod("Cos"), a)) }
                                                            );
            Console.WriteLine(express);

        }
    }

 不知是否正确,欢迎拍砖

运行界面如下:

 源码下载

 

转载于:https://www.cnblogs.com/kingxp/archive/2012/12/30/2840120.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值