I have a need to compile some code using CodeDomProvider.CompileAssemblyFromSource
. How would one go about debugging it? Basically, I want to compile it, create instance of a type and then step into the code for the type.
After I posted a question, I realized that my problem was that I was generating the assembly from string, not from file. I went back and changed the code to run with different options when in DEBUG and I am able to step right in from unit test code. Also one has 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
Interesting question. I think your best bet would be to use WinDbg to attach to the running .NET exe process (I think you will have to do this after the tyoe has been compiled in memory, since the memory addresses for the EXE will change - I assume).
Then, when the type is compiled and running in memory, you can then search for that type using commands found in SOS.dll. You can also place breakpoints in memory using SOS.dll
Getting started with SOS link
http://rionisimpsoni.wordpress.com/2009/10/08/getting-started-with-windbg-and-sos-dll/
This is a bit of light answer, since explaining how to use WinDbg and SOS.dll has been covered many times on the web.
Edit:
One of the cons of this method is that you won't be able to see the source code like Visual Studio displays it. You will see assembly language displayed as you step through the code. This might put you off already :), but if you stick with it, and understand a bit of assembly, you could do enough to debug errors for example.
Another thing you could do is dump the .NET assembly from memory into a file on disk. The SOS.dll command to do that escapes me now, I'll go look for it...
Ahh, it's SaveModule
. An example of this can be found in the comments here.
Did you try attaching to the process using your code from the Debug > Attach to process feature in VS?
© 2022 - 2024 — McMap. All rights reserved.