zoukankan      html  css  js  c++  java
  • 表达式:使用API创建表达式树(3)

    一、DebugInfoExpression:发出或清除调试信息的序列点。 这允许调试器在调试时突出显示正确的源代码。

            static void Main(string[] args)
            {
    
                var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("foo"), System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave);
    
                var mod = asm.DefineDynamicModule("mymod", true);
                var type = mod.DefineType("baz", TypeAttributes.Public);
                var meth = type.DefineMethod("go", MethodAttributes.Public | MethodAttributes.Static);
    
                var sdi = Expression.SymbolDocument("TestDebug.cs");
    
                var di = Expression.DebugInfo(sdi, 2, 2, 2, 12);
                var gen = DebugInfoGenerator.CreatePdbGenerator();
    
    
                var exp = Expression.Divide(Expression.Constant(2), Expression.Subtract(Expression.Constant(4), Expression.Constant(4)));
                var block = Expression.Block(di, exp);
    
                Expression.Lambda(block, new ParameterExpression[0]).CompileToMethod(meth, gen);
    
                var newtype = type.CreateType();
                asm.Save("tmp.dll");
                newtype.GetMethod("go").Invoke(null, new object[0]);
                Console.WriteLine(" ");
            }

    运行了下:

        未经处理的异常:  System.Reflection.TargetInvocationException: 调用的目标发生了异
        常。 ---> System.DivideByZeroException: 尝试除以零。
           在 baz.go() 位置 TestDebug.cs:行号 2
           --- 内部异常堆栈跟踪的结尾 ---
           在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
         Signature sig, Boolean constructor)
           在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec
        t[] parameters, Object[] arguments)
           在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
        Attr, Binder binder, Object[] parameters, CultureInfo culture)
           在 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
           在 ConsoleApplication2.Program.Main(String[] args) 位置 d:平台演示程序Debu
        gInfoExpressionApplication2ConsoleApplication2Program.cs:行号 72

    那个  “在 baz.go() 位置 TestDebug.cs:行号 2”就是DebugInfoExpression产生的效果,试了几种其他的方法,不能出现类似的效果,说Debug info 只会出现在编译过的方法。
    DebugInfoExpression用的机会也不多,就没深究了

    二、DefaultExpression :表示类型或空表达式的默认值。有点类似泛型的默认值操作,没什么难度:
    下面摘自MSDN

    Expression defaultExpr = Expression.Default(
                                typeof(byte)
                            );
    
    // 显示表达式
    Console.WriteLine(defaultExpr.ToString());
    
    // 创建表达式树,并执行
    Console.WriteLine(
        Expression.Lambda<Func<byte>>(defaultExpr).Compile()());
    
    // 显示结果:
    //
    // default(Byte)
    // 0
  • 相关阅读:
    shell-变量的数值运算let内置命令
    shell-变量的数值运算符-计算双括号(())的使用
    shell-批量修改文件名及扩展名多案例
    shell-变量的字串应用技术
    一个MVVM前端扩展器
    测试一个mysql 悲观锁
    spring rest项目提示Request method 'PUT' not supported Method Not Allowed 405 错误
    Mysql 使用sql语句添加列,修改列默认值,添加类注释
    理解java泛型中的 上界extend 下界super
    mysql存储过程游标循环装载字符串SQL语句示例
  • 原文地址:https://www.cnblogs.com/athinker/p/3596169.html
Copyright © 2011-2022 走看看