.net - DLR-为什么调试信息没有显示在我的堆栈跟踪中?

标签 .net linq c#-4.0

首先,我已经阅读Making a CLR/.NET Language Debuggable,但是仍然难以实现。

我编写了一种玩具语言,该语言可以通过生成Linq表达式,然后调用LambdaExpression#CompileToMethod来工作。这些表达式中的大多数都附有调试信息,如下所示:

//SmithExpression#InsertDebugInfo
Expression InsertDebugInfo(Expression expression, DebugInfo debugInfo) {
    var column = 1;
    var debugExpr = Expression.DebugInfo(debugInfo.SymbolDocumentInfo
                 ,Info.LineNumber, column, Info.LineNumber, column + 1);
    return Expression.Block(debugExpr, expression);
}

DebugInfo看起来像这样:
public class DebugInfo {
    /* arbitrary value from http://www.famkruithof.net/uuid/uuidgen */
    public static Guid SmithGuid = new Guid("83c65910-8376-11e2-9e96-0800200c9a66");

    public readonly SymbolDocumentInfo SymbolDocumentInfo;
    public readonly DebugInfoGenerator DebugPdbGenerator;

    public DebugInfo(String name) {
        SymbolDocumentInfo = Expression.SymbolDocument(name, SmithGuid);
        DebugPdbGenerator = DebugInfoGenerator.CreatePdbGenerator();
    }
}

整个过程像这样杂乱无章(您可以忽略有关init的部分):
public static Action CompileSmithExpression(SmithExpression sexpression
           ,DebugInfo debugInfo, Parameter moduleParameter, Expando module) {
    AssemblyName assemblyName = 
        new AssemblyName(
             "RuntimeHelpers.CompileToSmithExpression helper assembly"
          );
    AssemblyBuilder assemblyBuilder =
        AppDomain.CurrentDomain.DefineDynamicAssembly(
          assemblyName, AssemblyBuilderAccess.RunAndSave
        );

    ModuleBuilder moduleBuilder = assemblyBuilder
             .DefineDynamicModule(assemblyName.Name, "onlyModule.dll");

    var debugAttributes =
        DebuggableAttribute.DebuggingModes.Default |
        DebuggableAttribute.DebuggingModes.DisableOptimizations;

    ConstructorInfo constructor =
        typeof(DebuggableAttribute)
       .GetConstructor(new Type[] { 
           typeof(DebuggableAttribute.DebuggingModes)
           }
        );
    var cab = new CustomAttributeBuilder(constructor, new object[] { debugAttributes });
    assemblyBuilder.SetCustomAttribute(cab);
    moduleBuilder.SetCustomAttribute(cab);

    TypeBuilder typeBuilder = 
       moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);

    //inits generates expressions that set 'constant' fields to their values.
    //the call also adds the 'constant' fields to the typeBuilder.
    //Must call ToArray() to make it run.
    var inits = FieldInits(sexpression, typeBuilder).ToArray();
    var ex = sexpression.ToExpression(debugInfo);
    var fullDlrExpression = Expression.Block(inits.Append(ex));

    var parameters = new ParameterExpression[] { moduleParameter.DlrParameter };
    var lambda = Expression.Lambda(fullDlrExpression, parameters);

    /* Method will take the module as a parameter. */
    MethodBuilder meth = typeBuilder.DefineMethod(
        "MyMethod",
        MethodAttributes.Public | MethodAttributes.Static,
        typeof(void),
        new Type[] { typeof(Expando) } );

    lambda.CompileToMethod(meth, debugInfo.DebugPdbGenerator);

    Type madeType = typeBuilder.CreateType();

    return () => madeType.GetMethod("MyMethod").Invoke(null, new Object[] { module });
}

运行代码会给出我想要的异常,但不包括该表达式具有的调试信息。我想说类似“”的内容。
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MissingMemberException: Can't invoke member error of [] []
at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,Smith.Expando) <IL 0x0004f, 0x00127>
at System.Dynamic.UpdateDelegates.UpdateAndExecute1<Smith.Expando, object> (System.Runtime.CompilerServices.CallSite,Smith.Expando) <0x0040b>
at MyDynamicType.MyMethod (Smith.Expando) <IL 0x002bc, 0x00aaa>
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) <IL 0x00016, 0x00067>
etc...

我最好的猜测是调试信息确实存在,但是我必须做更多的工作才能使stacktrace展示出来。有任何想法吗?

最佳答案

您的异常是嵌套异常。要打印出堆栈跟踪,请查看InnerException。

catch (Exception ex)
{
    while (ex != null) {
        Debug.Print(ex.ToString());
        ex = ex.InnerException();
    }
}

关于.net - DLR-为什么调试信息没有显示在我的堆栈跟踪中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15593223/

相关文章:

c# - 无后端 API - 创建用户

c# - 如何在SQL参数中添加多个 "WHERE conditionA OR conditionB OR conditionC"?

c# - 满足特定条件的 Array.BinarySearch

wpf - 如何使用 WPF 和 MVVM 将数据库中的数据加载到 DataGrid?

c# - Excel Addin 中的 Application.ActiveWorkbook 为空

c# - 单元测试对象类型的正确方法

Linq to SQL 和 Entity Framework 的区别?

c# - linq paging - 获取总行数

c# - 四舍五入到两位小数

c# - 使用 "where": Cannot convert lambda expression to type 'bool'