CodeDomProvider.CompileAssemblyFromSource-找不到Roslyn(csc.exe) [英] CodeDomProvider.CompileAssemblyFromSource - Can't Find Roslyn (csc.exe)

查看:216
本文介绍了CodeDomProvider.CompileAssemblyFromSource-找不到Roslyn(csc.exe)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We recently upgraded from an old CodeDomProvider to the new Roslyn CodeDomProvider called Microsoft.CodeDom.Providers.DotNetCompilerPlatform. It works fine, but it looks for the csc.exe in the wrong place. The NuGet package puts the exe in the path:

[App Path] \bin\Debug\roslyn

[App Path]\bin\Debug\roslyn

但是,当我们编译时,出现以下错误:
找不到路径'[App Path] \bin\Debug\bin\roslyn\csc的一部分。

But, when we compile, we get this error: Could not find a part of the path '[App Path]\bin\Debug\bin\roslyn\csc.exe'.

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

Notice that it is looking for the exe in the wrong place. It's looking for it inside the "bin" folder which is already in the bin\Debug folder. So, in order to make our code compile, we need to move the Roslyn compiler to: [App Path]\bin\Debug\bin\roslyn\csc.exe

有没有办法告诉CodeDomProvider Roslyn编译器位于哪里?

Is there any way to tell the CodeDomProvider where the Roslyn compiler is located? Isn't this just a straight up bug in the Roslyn compiler code?

推荐答案

我会看一下NuGet软件包吗? Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix。我没有使用过它,但下载量为1万,因为我认为这是很多人遇到的问题。我遇到了这个问题,我记得使用反射来解决此问题,以下是我基于它所基于的Stack Overflow答案编写的代码段,其中_compiler是我的CSharpCodeProvider:

I'd take a look at the NuGet package Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix. I haven't used it but it has 10K downloads because this is a problem a lot of people have run into, I think. I ran into this problem and I remember using reflection to workaround it, here's the snippet I wrote with a reference to the Stack Overflow answer I was basing it on, where _compiler is my 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);

这篇关于CodeDomProvider.CompileAssemblyFromSource-找不到Roslyn(csc.exe)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆