Debugging a generated .NET assembly from within the application that generated it

前端 未结 2 1496
说谎
说谎 2021-02-04 07:50

The question in short: How can I debug the code generated during a debugging session on the generating program? (see code below)

I am facing the following issue: I would

相关标签:
2条回答
  • 2021-02-04 08:29

    Visual Studio 2010 handles this gently in the debugger. I was surprised of it after I upgraded. I hope it helps.

    0 讨论(0)
  • 2021-02-04 08:32

    I finally found a way to workaround it after discovering that my question was a duplicate of How to debug/break in codedom compiled code, which was not obvious for me to find. bbmud gives a very good hint in there to get the debugger working correctly, but doesn't tell how to get into the code. I add a reference to some assembly containing an interface that I want to implement in the scripts:

    compilerParams.ReferencedAssemblies.Add(typeof(IPlugin).Assembly.Location);
    compilerParams.GenerateExecutable = false; // generate the DLL
    
    // if you want to debug, this is needed...
    compilerParams.GenerateInMemory = false;
    compilerParams.TempFiles = new TempFileCollection(Environment.
          GetEnvironmentVariable("TEMP"), true);
    

    Now when I consider CSharpFriends being an implementation of IPlugin, I can get the interface by casting the obj above:

    IPlugin script = obj as IPlugin;
    

    Then debugging calls to interface methods or properties is as easy as usual! The trick of adding

     System.Diagnostics.Debugger.Break();
    

    inside the script code also works well but it needs change to the script. As the code inside the application always needs to know what kind of methods are inside the script according to some mechanism (reflexion with attributes or interfaces), using an interface known by both is a very acceptable solution for me.

    I hope it helps somebody else.

    0 讨论(0)
提交回复
热议问题