views:

209

answers:

1

Not quite the same as this thread, but pretty close.

My program allows people to enter some VB or C# code which gets compiled, loaded and executed at runtime. My CompilerParams are:

CompilerParameters params = new CompilerParameters();
params.GenerateExecutable = false;
params.GenerateInMemory = true;
params.IncludeDebugInformation = false;
params.TreatWarningsAsErrors = false;
params.WarningLevel = 4;

When this code throws an exception I'd like to be able to display a message box that helps users debug their code. The exception message is easy, but the line-number is where I got stuck.

I suspect that in order to get at the line number, I may need to drastically change the CompilerParameters and perhaps even the way these dlls get stored/loaded.

Does anyone know the least steps needed to get this to work?

+1  A: 

set OutputAssembly to a temp file, set GenerateInMemory = false, IncludeDebugInformation = true
That should generate symbols and allow you to get a full stack trace with code lines

Tom Frey
Thanks Tom, I was afraid that was the answer. Now I have to figure out a way to delete all those dll and pdb files *after* my application shuts down... Any ideas on that?
David Rutten
just create them in the Windows Temp directory so windows takes care of it: Path.GetTempFileName()
Tom Frey
That's where I put them, but that folder is close to 1 Gig on my machine, if someone is taking care of it they're doing a sloppy job...
David Rutten
hm, Windows might not clean that up until it 'feels' it has to. You could always create a separate folder and set the OutputAssembly = "mytempfolder" + Path.GetRandomFileName() Then just clean it up everytime the application launches or shuts down
Tom Frey
That's a good solution. The File delete will fail if there is still an instance of my app that is using a dll, so I can wipe all unused dlls in my own temp folder on run.
David Rutten