Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No line numbers on exceptions from assembly generated by CompileAssemblyFromSource

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 });
like image 913
Don Kirkby Avatar asked Jan 01 '26 06:01

Don Kirkby


2 Answers

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.

like image 85
Jeff Yates Avatar answered Jan 03 '26 21:01

Jeff Yates


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.

like image 22
Don Kirkby Avatar answered Jan 03 '26 22:01

Don Kirkby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!