I have an application which loads up c# source files dynamically and runs them as plugins. When I am running the main application in debug mode, is it possible to debug into the dynamic assembly? Obviously setting breakpoints is problematic, since the source is not part of the original project, but should I be able to step into, or break on exceptions for the code?
我有一个应用程序动态加载c#源文件并将其作为插件运行。当我在调试模式下运行主应用程序时,是否可以调试到动态程序集?显然设置断点是有问题的,因为源不是原始项目的一部分,但是我应该能够进入或打破代码的异常吗?
Is there a way to get codedom to generate PDBs for this or something?
有没有办法让编码器生成这个或什么的PDB?
Here is the code I am using for dynamic compliation.
这是我用于动态compliation的代码。
CSharpCodeProvider codeProvider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
//codeProvider.
ICodeCompiler icc = codeProvider.CreateCompiler();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
parameters.CompilerOptions = string.Format("/lib:\"{0}\"", Application.StartupPath);
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("System.Core.dll");
CompilerResults results = icc.CompileAssemblyFromSource(parameters, Source);
DLL.CreateInstance(t.FullName, false, BindingFlags.Default, null, new object[] { engine }, null, null);
2 个解决方案
#1
32
Try the following options:
请尝试以下选项:
parameters.GenerateInMemory = false; //default
parameters.TempFiles = new TempFileCollection(Environment.GetEnvironmentVariable("TEMP"), true);
parameters.IncludeDebugInformation = true;
I am not sure if this works OK in your case, but if it does, you can surround this parameters with conditional compilation directive, so that it dumps the generated assembly only in debug mode.
我不确定这是否适用于您的情况,但如果确实如此,您可以使用条件编译指令包围此参数,以便它仅在调试模式下转储生成的程序集。
#2
8
The answer by @bbmud is correct, though it misses one bug fix. The CSharpCodeGenerator (the class in .NET the compiles C# code to IL) is set to remove pdb files immediately after they are created, UNLESS you add /debug:pdbonly
to the CompilerOptions string. However, if you do that, the IncludeDebugInformation
flag is ignored and the compiler generates optimised code which is hard to debug. To avoid this you must explicitly tell the Code Generator to keep all files.
@bbmud的答案是正确的,虽然它错过了一个错误修复。 CSharpCodeGenerator(.NET中的类将C#代码编译为IL)设置为在创建pdb文件后立即删除它们,除非您向CompilerOptions字符串添加/ debug:pdbonly。但是,如果这样做,IncludeDebugInformation标志将被忽略,编译器将生成难以调试的优化代码。为避免这种情况,您必须明确告诉代码生成器保留所有文件。
Here is the complete recipe:
这是完整的食谱:
parameters.GenerateInMemory = false; //default
parameters.TempFiles = new TempFileCollection(Environment.GetEnvironmentVariable("TEMP"), true);
parameters.IncludeDebugInformation = true;
parameters.TempFiles.KeepFiles = true
Here is the culprit part of the code of CSharpCodeGenerator:
这是CSharpCodeGenerator代码的罪魁祸首:
string fileExtension = "pdb";
if ((options.CompilerOptions != null) && (CultureInfo.InvariantCulture.CompareInfo.IndexOf(options.CompilerOptions, "/debug:pdbonly", CompareOptions.IgnoreCase) != -1))
{
results.TempFiles.AddExtension(fileExtension, true);
}
else
{
results.TempFiles.AddExtension(fileExtension);
}
The TempFiles.AddExtension(fileExtension, true)
tells the compile to keep the pdb files. The else option of results.TempFiles.AddExtension(fileExtension);
tells it to treat pdb as all temporary files which by default means delete them.
TempFiles.AddExtension(fileExtension,true)告诉编译器保留pdb文件。 results.TempFiles.AddExtension(fileExtension)的else选项;告诉它将pdb视为所有临时文件,默认情况下是删除它们。
#1
32
Try the following options:
请尝试以下选项:
parameters.GenerateInMemory = false; //default
parameters.TempFiles = new TempFileCollection(Environment.GetEnvironmentVariable("TEMP"), true);
parameters.IncludeDebugInformation = true;
I am not sure if this works OK in your case, but if it does, you can surround this parameters with conditional compilation directive, so that it dumps the generated assembly only in debug mode.
我不确定这是否适用于您的情况,但如果确实如此,您可以使用条件编译指令包围此参数,以便它仅在调试模式下转储生成的程序集。
#2
8
The answer by @bbmud is correct, though it misses one bug fix. The CSharpCodeGenerator (the class in .NET the compiles C# code to IL) is set to remove pdb files immediately after they are created, UNLESS you add /debug:pdbonly
to the CompilerOptions string. However, if you do that, the IncludeDebugInformation
flag is ignored and the compiler generates optimised code which is hard to debug. To avoid this you must explicitly tell the Code Generator to keep all files.
@bbmud的答案是正确的,虽然它错过了一个错误修复。 CSharpCodeGenerator(.NET中的类将C#代码编译为IL)设置为在创建pdb文件后立即删除它们,除非您向CompilerOptions字符串添加/ debug:pdbonly。但是,如果这样做,IncludeDebugInformation标志将被忽略,编译器将生成难以调试的优化代码。为避免这种情况,您必须明确告诉代码生成器保留所有文件。
Here is the complete recipe:
这是完整的食谱:
parameters.GenerateInMemory = false; //default
parameters.TempFiles = new TempFileCollection(Environment.GetEnvironmentVariable("TEMP"), true);
parameters.IncludeDebugInformation = true;
parameters.TempFiles.KeepFiles = true
Here is the culprit part of the code of CSharpCodeGenerator:
这是CSharpCodeGenerator代码的罪魁祸首:
string fileExtension = "pdb";
if ((options.CompilerOptions != null) && (CultureInfo.InvariantCulture.CompareInfo.IndexOf(options.CompilerOptions, "/debug:pdbonly", CompareOptions.IgnoreCase) != -1))
{
results.TempFiles.AddExtension(fileExtension, true);
}
else
{
results.TempFiles.AddExtension(fileExtension);
}
The TempFiles.AddExtension(fileExtension, true)
tells the compile to keep the pdb files. The else option of results.TempFiles.AddExtension(fileExtension);
tells it to treat pdb as all temporary files which by default means delete them.
TempFiles.AddExtension(fileExtension,true)告诉编译器保留pdb文件。 results.TempFiles.AddExtension(fileExtension)的else选项;告诉它将pdb视为所有临时文件,默认情况下是删除它们。