No line numbers for assembly exceptions generated by CompileAssemblyFromSource

My application has a scripting function where I compile an in-memory assembly from a user script using CodeDomProvider.CompileAssemblyFromSource. This is similar to what is described in this answer .

It works very well, but any exceptions that are thrown from the script code do not have line numbers in the stack trace. I tried setting compilerParameters.IncludeDebugInformation = true, but it still does not include line numbers.

Is it possible to get line numbers in exceptions from an assembly in memory?

Here are the key code fragments that I use 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 });
+3
3

, , , . , .

+4

, PDB , , , . , .

+2

In an application developed for the .NET Framework 4, I get line numbers in the exception stack trace when the system I'm running on has the .NET Framework 4.5 (or presumably later), but I don't get them when it is only the .NET Framework 4 (full or client profile).

So, yes, now you can get the line numbers in the exceptions from the assembly in memory, namely by running the .NET Framework 4.5.

0
source

Source: https://habr.com/ru/post/1709112/


All Articles