Expression.DebugInfo 如何标记表达式?

发布于 2024-11-25 11:17:46 字数 1534 浏览 3 评论 0原文

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

    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”文件

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(" ");
        }
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自←快乐 2024-12-02 11:17:47

所以看来我错过了调试信息生成器。需要添加此代码:

    var gen = DebugInfoGenerator.CreatePdbGenerator();

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

它现在就像魅力一样!

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

It works like a charm now!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文