2011-07-20 17 views
7

Vì vậy, tôi biết những gì Expression.DebugInfo được sử dụng cho, và tôi có một biểu thức gỡ lỗi được tạo ra, nhưng làm thế nào để tôi tag các biểu thức khác với thông tin gỡ lỗi này? Dưới đây là những gì tôi đang cố gắng làm thử nghiệm cơ bản:Expression.DebugInfo Làm cách nào để biểu thị thẻ?

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

Tôi biết Thông tin gỡ lỗi chỉ hoạt động cho các phương pháp được biên dịch vì vậy đó là lý do tôi tạo lắp ráp khi đang di chuyển. Nhưng khi mã này gây ra lỗi "chia cho số không", nó không hiển thị tệp "TestDebug.txt" của tôi

Trả lời

8

Vì vậy, có vẻ như tôi thiếu trình tạo thông tin gỡ lỗi. Mã này cần được thêm vào:

var gen = DebugInfoGenerator.CreatePdbGenerator(); 

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

Nó hoạt động như một sự quyến rũ ngay bây giờ!

Các vấn đề liên quan