2011-07-20 12 views
7

Więc wiem co Expression.DebugInfo służy do i mam wyrazem Debug utworzone, ale jak ja oznaczyć inne wyrażenia z tej informacji debugowania? Oto, co usiłuję jako naprawdę podstawowego badania:Expression.DebugInfo Jak wyszczególniać wyrażenia?

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

Wiem informacji Debug działa tylko dla opracowanych metod więc dlatego jestem generowania zespół w locie. Ale kiedy ten kod powoduje „dzielenie przez zero” błąd, to nie pokazując mój mój „TestDebug.txt” plik

Odpowiedz

8

Więc wydaje mi brakuje generator informacji debugowania. Ten kod musiał być dodany:

var gen = DebugInfoGenerator.CreatePdbGenerator(); 

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

Teraz działa jak urok!