CompileAssemblyFromSource生成的汇编异常中没有行号

时间:2021-12-26 00:28:44

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.

我的应用程序有一个脚本功能,我使用CodeDomProvider.CompileAssemblyFromSource从用户的脚本编译内存中的程序集。它类似于这个答案中描述的内容。

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.

它工作得很好,但是从脚本代码中抛出的任何异常在堆栈跟踪中都没有行号。我尝试设置compilerParameters.IncludeDebugInformation = true,但它仍然不包括行号。

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 });

3 个解决方案

#1


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.

我们通过将源写入临时文件然后使用该文件来编译代码而不是内存中的字符串来解决这个问题。这为我们提供了有意义的调试信息,否则我们无法获得。

#2


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.

从我在这里和这里发现的评论看起来,组件的PDB文件必须与附加行号之前的程序集在同一目录中。这似乎排除了内存中程序集的调试信息。

#3


In an application developed for .NET Framework 4, I do get line numbers in the exception stack traces when the system I am running on has .NET Framework 4.5 (or presumably later) installed, but I do not get them when it only has .NET Framework 4 (Full or Client Profile).

在为.NET Framework 4开发的应用程序中,当我运行的系统安装了.NET Framework 4.5(或者可能是稍后版本)时,我确实在异常堆栈跟踪中获得了行号,但是当它只有安装时我没有得到它们。 NET Framework 4(完整或客户端配置文件)。

So yes, it is possible to get line numbers on exceptions from an in-memory assembly now, namely by running on .NET Framework 4.5.

所以是的,现在可以从内存中的程序集中获取行数,即通过在.NET Framework 4.5上运行。

#1


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.

我们通过将源写入临时文件然后使用该文件来编译代码而不是内存中的字符串来解决这个问题。这为我们提供了有意义的调试信息,否则我们无法获得。

#2


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.

从我在这里和这里发现的评论看起来,组件的PDB文件必须与附加行号之前的程序集在同一目录中。这似乎排除了内存中程序集的调试信息。

#3


In an application developed for .NET Framework 4, I do get line numbers in the exception stack traces when the system I am running on has .NET Framework 4.5 (or presumably later) installed, but I do not get them when it only has .NET Framework 4 (Full or Client Profile).

在为.NET Framework 4开发的应用程序中,当我运行的系统安装了.NET Framework 4.5(或者可能是稍后版本)时,我确实在异常堆栈跟踪中获得了行号,但是当它只有安装时我没有得到它们。 NET Framework 4(完整或客户端配置文件)。

So yes, it is possible to get line numbers on exceptions from an in-memory assembly now, namely by running on .NET Framework 4.5.

所以是的,现在可以从内存中的程序集中获取行数,即通过在.NET Framework 4.5上运行。