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.