Expression.DebugInfo如何标记表达式? [英] Expression.DebugInfo How Do I Tag Expressions?

查看:236
本文介绍了Expression.DebugInfo如何标记表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道用什么Expression.DebugInfo,并且我创建了一个Debug表达式,但是如何使用这个调试信息标记其他表达式?这是我正在尝试的一个真正的基本测试:

So I know what Expression.DebugInfo is used for, and I have an Debug expression created, but how to I tag other expressions with this debug info? Here's what I'm trying as a really basic test:

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

namespace ExpressionDebugTest
{
    class Program
    {
        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.txt");

            var di = Expression.DebugInfo(sdi, 2, 2, 2, 12);


            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);

            var newtype = type.CreateType();
            asm.Save("tmp.dll");
            newtype.GetMethod("go").Invoke(null, new object[0]);
            //meth.Invoke(null, new object[0]);
            //lambda.DynamicInvoke(new object[0]);
            Console.WriteLine(" ");
        }
    }
}

我知道调试信息只能工作对于编译方法,这就是为什么我正在生成一个程序集。但是,当这段代码导致除以零错误时,它不会显示我的TestDebug.txt文件

I know Debug info only works for compiled methods so that's why I'm generating an assembly on the fly. But when this code causes a "divide by zero" error, it's not showing my my "TestDebug.txt" file

推荐答案

似乎我错过了调试信息生成器。此代码需要添加:

So it seems I was missing the debug info generator. This code needed to be added:

    var gen = DebugInfoGenerator.CreatePdbGenerator();

    Expression.Lambda(block, new ParameterExpression[0]).CompileToMethod(meth,gen);

它现在就像一个魅力!

这篇关于Expression.DebugInfo如何标记表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆