[英]c# getting Internal error in the expression evaluator when programmatically compiling code with Roslyn
我已经建立了一个控制台项目,该控制台项目基于我拥有的一些非常简单的模板在构建时编译代码。 模板只是设置从另一个项目的基类继承的类。 我能够运行代码并逐行浏览基类,但是当我尝试查看属性的值是什么时,我Internal error in the expression evaluator
遇到了Internal error in the expression evaluator
。 我尝试设置控制台项目以使用调试配置编译所有内容:
CSharpCompilationOptions DefaultCompilationOptions =
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithOverflowChecks(true)
.WithOptimizationLevel(OptimizationLevel.Debug)
.WithOutputKind(OutputKind.DynamicallyLinkedLibrary)
.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);
CSharpCompilation compilation = CSharpCompilation.Create(
name,
syntaxTrees: syntaxTrees,
references: GetMetaDataReferences(context, moduleBinPath),
options: DefaultCompilationOptions);
try
{
var ms = new MemoryStream();
var pdbStream = new MemoryStream();
var docStream = new MemoryStream();
var result = compilation.Emit(ms, pdbStream, docStream);
if (!result.Success)
{
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in failures)
{
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
}
else
{
CreateFile($"{moduleBinPath}\\{name}.dll", ms);
CreateFile($"{moduleBinPath}\\{name}.pdb", pdbStream);
CreateFile($"{moduleBinPath}\\{name}.xml", docStream);
}
ms.Dispose();
pdbStream.Dispose();
docStream.Dispose();
}
catch (Exception ex)
{
Console.Error.WriteLine("Could not compile assembly!!");
Console.Error.WriteLine(ex.ToString());
}
然后在启动时加载dll:
string scriptPath = $"{moduleBinPath}\\{moduleAssembly}.Scripts.dll";
if (File.Exists(scriptPath))
{
Assembly.LoadFile(scriptPath)
AssemblyLoadContext.Default.Resolving += (loadContext, name) =>
{
if (!name.Name.Equals($"{moduleAssembly}.Scripts")) return null;
return loadContext.LoadFromAssemblyPath(scriptPath);
};
}
我可以看到这些符号也已加载到“模块”窗口中。 为了使调试正常工作,我还需要做其他事情吗?
我实际上最终弄清楚了这一点,最大的问题是我正在使用Assembly.LoadFile
而不是AssemblyLoadContext.Default.LoadFromAssemblyPath
。 后者在加载插件的依赖程序集方面要好得多。
经过更多研究之后,我偶然发现了这篇文章 ,继续讨论Assembly.LoadFile的可靠性如何。 很难确定问题的确切原因,但是我假设可能存在某些程序集版本不匹配,从而导致表达式求值器无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.