Is it possible to debug code compiled at runtime?

I need to compile some code using CodeDomProvider.CompileAssemblyFromSource . How can I debug it? Basically, I want to compile it, instantiate the type, and then go into the code for the type.

+8
compiler-construction c # visual-studio-2010 codedom
source share
3 answers

After I posted the question, I realized that my problem is that I generate the assembly from a string, not from a file. I came back and changed the code to work with different parameters when in DEBUG, and I can go directly from the unit test code. It is also necessary to set GenerateInMemory to false and IncludeDebugInformation to true.

 #if DEBUG @params.IncludeDebugInformation = compilationContext.IncludeDebugInformation; @params.GenerateInMemory = compilationContext.GenerateInMemory; var fileName = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,@"..\..\" + compilationContext.AssemblyOutputName + ".cs")); File.WriteAllText(fileName,compilationContext.StringToCompile); return _codeDomProvider.CompileAssemblyFromFile(@params,fileName); #else return _codeDomProvider.CompileAssemblyFromSource(@params, compilationContext.StringToCompile); #endif 
+7
source share

Interest Ask. I believe that it is best to use WinDbg to join the .NET exe executable process (I think you will need to do this after tyoe has been compiled in memory since the memory addresses for the EXE will change - I suppose).

Then, when the type is compiled and run in memory, you can search for this type using the commands found in SOS.dll. You can also place breakpoints in memory using SOS.dll

Getting started with an SOS link

http://rionisimpsoni.wordpress.com/2009/10/08/getting-started-with-windbg-and-sos-dll/

This is a slightly easy answer, as the explanation of how to use WinDbg and SOS.dll has been repeatedly viewed on the Internet.

Edit:

One of the disadvantages of this method is that you cannot see the source code, such as Visual Studio. When you go through the code, you will see assembly language. This may put you off already :), but if you stick to it and understand a little assembly, you could do enough to debug the errors, for example.

Another thing you could do is to unload the .NET assembly from memory to a file on disk. The SOS.dll command for this eludes me, I will go after it ...

Ahh, this is SaveModule . An example of this can be found in the comments here .

+3
source share

Have you tried connecting to a process using your code from the Debug> Attach to process function in VS?

0
source share

All Articles