Color Of Code

Font Size




Menu Style



Debugging inside a generated .NET assembly

.NET assemblies can be generated or compiled from a string directly in memory. This is a great thing as it enables you to make advanced scripting available to your users. They have the full features of C# or any other .NET language and you can bind the generated assembly into your application. This is well known, but there are some difficulties I encountered regarding debugging.

How can one debug the code generated while debugging the generating program? The methods or classes are not known to the application running the script. Using a MethodInvoker lets the code run but you cannot step into it.

Sample code

-< work in progress >-

See my question on stack overflow.


Some requirements must be met to enable debugging inside such generated assemblies. First of all, the symbols must be available, as well as the source code.

Calling Break

One possible solution involves the introduction of System.Diagnostics.Debug.Break(); into the script. This works well, the debugger stops at the place where the break was put. But this method is invasive: you have to modify the code of the script to make it work.

Using interfaces

Another way is to agree about a contract, an interface that both application and script know. One instanciated object that implements the interface can be manipulated by using the interface methods. By stepping into a call to one method, the debugger is able to resolve the source code and display the next line of the generated assembly. You can debug as usual having full debugging capabilities.

Visual Studio 2010

To my surprise VS 2010 handles the debugging in assemblies generated in memory cleanly. You have the full debugging options.