所以我知道 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”文件
所以看来我错过了调试信息生成器。需要添加这段代码:
var gen = DebugInfoGenerator.CreatePdbGenerator();
Expression.Lambda(block, new ParameterExpression[0]).CompileToMethod(meth,gen);
现在它就像一个魅力!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)