zoukankan      html  css  js  c++  java
  • 表达式:使用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()());
  • 相关阅读:
    文件类型的判断
    VS 2003 源码配置管理(subversion+apache)
    DataView
    sql server 挂起的文件操作
    关于权限设计的轻量级实现
    各种类型文件在SQL Server中存储的解决方案
    免费或开源的项目管理工具
    UML中的用例(Use Case)概念分析及实例
    大对象的存储
    用js实现同一页面多个运动效果
  • 原文地址:https://www.cnblogs.com/athinker/p/3593736.html
Copyright © 2011-2022 走看看