Expression.DebugInfo 如何标记表达式?
所以我知道 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以看来我错过了调试信息生成器。需要添加此代码:
它现在就像魅力一样!
So it seems I was missing the debug info generator. This code needed to be added:
It works like a charm now!