CodeDomProvider.CompileAssemblyFromSource - 找不到 Roslyn (csc.exe)



我们最近从一个旧的编码模式升级到新的Roslyn CodedomProvider,称为Microsoft.codedom.providers.dotnetcompilerplatform。它可以正常工作,但是它在错误的位置寻找csc.exe。Nuget软件包将EXE放在路径中:

[app path] bin debug roslyn

但是,当我们编译时,我们会收到此错误:找不到路径的一部分'[app path] bin debug bin roslyn csc.exe'。

请注意,它正在寻找错误的位置。它正在在" bin"文件夹中寻找它,该文件夹已经在bin debug文件夹中。因此,为了使我们的代码编译,我们需要将Roslyn编译器移至:[app path] bin debug bin roslyn csc.exe

有什么方法可以告诉编码词者罗斯林编译器在哪里?这不是Roslyn编译器代码中的直接错误吗?

我会看一下nuget软件包microsoft.codedom.providers.dotnetcompilerplatform.binfix。我没有使用过它,但是它有10k下载,因为这是很多人遇到的问题。我遇到了这个问题,我记得在使用反射来解决问题,这是我写的片段,引用了我正在基于的堆栈溢出答案,其中_compiler是我的csharpcodeprovider:

// Little hack here, see http://stackoverflow.com/a/40311406/1676558.
object compilerSettings = typeof(CSharpCodeProvider)
    .GetField("_compilerSettings", BindingFlags.Instance | BindingFlags.NonPublic)
    .GetValue(_compiler);
FieldInfo compilerSettingsFullPathField = compilerSettings
    .GetType()
    .GetField("_compilerFullPath", BindingFlags.Instance | BindingFlags.NonPublic);
string desiredCompilerSettingsFullPath = ((string)compilerSettingsFullPathField
    .GetValue(compilerSettings))
    .Replace(@"binroslyn", @"roslyn");
compilerSettingsFullPathField.SetValue(compilerSettings, desiredCompilerSettingsFullPath);

更改'构建事件命令行'in'构建项目设置的'选项卡

IF EXIST $(TargetDir)roslyncsc.exe (MKDIR $(TargetDir)bin & MOVE /Y $(TargetDir)roslyn $(TargetDir)binroslyn)

最新更新