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

我们最近从旧的CodeDomProvider升级到名为Microsoft.CodeDom.Providers.DotNetCompilerPlatform的新Roslyn CodeDomProvider。 它工作正常,但它在错误的地方寻找csc.exe。 NuGet包将exe放在路径中:

[App Path] \ bin \ Debug \ roslyn

但是,当我们编译时,我们得到这个错误: 无法找到路径'[App Path] \ bin \ Debug \ bin \ roslyn \ csc.exe’的一部分。

请注意,它正在寻找错误位置的exe。 它正在bin \ Debug文件夹中的“bin”文件夹中查找它。 因此,为了使我们的代码编译,我们需要将Roslyn编译器移动到:[App Path] \ bin \ Debug \ bin \ roslyn \ csc.exe

有没有办法告诉CodeDomProvider Roslyn编译器所在的位置? 这不仅仅是Roslyn编译器代码中的一个直接错误吗?

我将看看NuGet包Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix。 我没有使用它,但它有10K的下载量,因为这是很多人遇到的问题,我想。 我遇到了这个问题并且我记得使用reflection来解决它,这里是我写的片段,其中引用了Stack Overflow的答案我基于它,其中_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(@"bin\roslyn\", @"roslyn\"); compilerSettingsFullPathField.SetValue(compilerSettings, desiredCompilerSettingsFullPath); 

将项目设置的“ 构建事件 ”选项卡中的“ 构建 后事件命令行 ”更改为:

 IF EXIST $(TargetDir)roslyn\csc.exe (MKDIR $(TargetDir)bin & MOVE /Y $(TargetDir)roslyn $(TargetDir)bin\roslyn)