表达式:使用API创建表达式树(2)

一、BlockExpression类:表式一个包含可在其中定义变量的表达式序列的块。是一组表达式,类似于多个委托的 += 后的效果,其返回表达式是最后一个表达式决定。
以下是BlockExpression的例子(取自官网中的例子,略改了下)

         //声明一个包含有4个表达式的表达式块。
      BlockExpression blockExpr = Expression.Block(Expression.Call(null,
        typeof(Console).GetMethod("Write", new Type[] { typeof(String) }), Expression.Constant("你好!")),// 1
        Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("BlockExpression表达式!")),    // 2
        Expression.Constant(42),                            //3
        Expression.Call(null, typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) }),
        Expression.Constant("测试1 "), Expression.Constant("测试2")) //4
        );
        Console.WriteLine("***********************");
            Console.WriteLine("显示表达示块的执行结果:");

            // 首先创建表达式树,
            // 编译,执行.           
            var result = Expression.Lambda<Func<string>>(blockExpr).Compile()();

            // 显示表达式块各表达式.
            Console.WriteLine("表达式块中的各表达式:");
            foreach (var expr in blockExpr.Expressions)
                Console.WriteLine(expr.ToString());

            // 表达式返回值
            Console.WriteLine("表达式返回值");
            Console.WriteLine(result);

        //***********************
            // 显示表达示块的执行结果:
            //你好!BlockExpression表达式!
            //表达式块中的各表达式:
            //Write("你好!")
            //WriteLine("BlockExpression表达式!")
            //42
            //Concat("测试1 ", "测试2")
            //表达式返回值
            //测试1 测试2

二、CatchBlock类:表示 try 块中的 catch 语句。
  单独的CatchBlock是没有意义的,他必需和TryExpression一起使用才有用。
  TryExpression类:表示 try/catch/finally/fault 块。
  构建如下表达式:(try {} Catch {})

  

            TryExpression tryCatchExpr = Expression.TryCatch(
        Expression.Block(
            Expression.Divide(Expression.Constant(50), Expression.Constant(2)),
            Expression.Constant("无异常返回")
        ),
        Expression.Catch(
            typeof(DivideByZeroException),
            Expression.Constant("异常返回")
        )
              );
            Console.WriteLine(Expression.Lambda<Func<string>>(tryCatchExpr).Compile()());

  //输出为 :无异常返回

  将上面的  Expression.Constant(2),更改为   Expression.Constant(0)

 

//输出为:异常返回

  如果无法判断异常类型,刚将typeof(DivideByZeroException),换成  typeof(Exception);

  如要增加 Finally,则用 TryCatchFinally 构建:

    TryExpression tryCatchExpr2 = Expression.TryCatchFinally(
           Expression.Block(
           Expression.Divide(Expression.Constant(50), Expression.Constant(2)),
           Expression.Constant("无导常返回")
           ),
           Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
           Expression.Constant("Finally 块")),
           Expression.Catch(typeof(Exception),

           Expression.Constant("导常返回")
           )
       );
    Console.WriteLine(Expression.Lambda<Func<string>>(tryCatchExpr2).Compile()());

 

 

 

 

 

 

 

posted @ 2014-03-11 13:35  Tikyang  阅读(442)  评论(0编辑  收藏  举报