My application has a scripting feature where I compile an in-memory assembly from the user's script using CodeDomProvider.CompileAssemblyFromSource. It's similar to what's described in this answer.
It works very well, but any exceptions that get thrown from the script code don't have line numbers in the stack trace. I tried setting the compilerParameters.IncludeDebugInformation = true, but it still doesn't include line numbers.
Is it possible to get line numbers on exceptions from an in-memory assembly?
Here are the key pieces of code I'm using to compile the assembly:
CompilerParameters compilerParameters =
compilerInfo.CreateDefaultCompilerParameters();
compilerParameters.GenerateInMemory = true;
compilerParameters.GenerateExecutable = false;
compilerParameters.IncludeDebugInformation = true;
...
CodeDomProvider codeProvider = compilerInfo.CreateProvider();
CompilerResults compilerResults =
codeProvider.CompileAssemblyFromSource(
compilerParameters,
new string[] { sourceCode });
We worked around this by writing the source out to a temporary file and then using that file to compile the code rather than an in-memory string. This provided us with meaningful debug information that we otherwise couldn't get.
From comments I found here and here, it looks like the PDB file for the assembly has to be in the same directory as the assembly before the line numbers will be attached. It seems like this rules out debugging info for in-memory assemblies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With