使用 Roslyn 以 .NET 3.5 为目标时手动编译 C#(csc.exe)

Compiling C# manually while targetting .NET 3.5 with Roslyn(csc.exe)

我正在使用 csc/csc2.exe 手动编译应用程序。我需要引用 .NET 3.5 dll,但似乎编译器也会自动添加 .NET 4.0 dll(这会导致冲突)。

我正在手动引用所需版本的 mscorlib 和其他系统 dll。从 Visual Studio 内部编译成功,但是从命令提示符手动编译相同的响应文件失败。

/nostdlib+
/platform:AnyCPU
/errorendlocation
/highentropyva-
/reference:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll"
/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll"
/reference:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll"
/debug+
/debug:full
/out:obj\Debug\Target.exe
/ruleset:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets\MinimumRecommendedRules.ruleset"
/target:exe
/utf8output
Program.cs Properties\AssemblyInfo.cs

对于每个引用的程序集,我都收到以下错误:

error CS1703: Multiple assemblies with equivalent identity have been imported: 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.dll' and 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll'. Remove one of the duplicate references.

是什么原因导致编译器包含 .NET 4.0 dll,我该怎么做才能防止它包含它们?

更新: 我不能使用 MSBUILD,因为我已经有来自应用程序的响应文件(不是项目文件)。该应用程序使用了过时版本的编译器,我正在尝试用支持 C#6 的更新版本替换他们的编译器。 我已经使用 Mono MCS 编译器成功完成了这项任务,但无法与 Roslyn 一起使用。我无法使用 MCS 编译器,因为它还不支持所有 C#6 功能。

使用 MSBUILD 的唯一方法是将响应文件解析回 C# 项目文件。这对我来说似乎是过度设计。

/noconfig 选项添加到命令行,它应该会按预期工作。

The /noconfig option tells the compiler not to compile with the csc.rsp file, which is located in and loaded from the same directory as the csc.exe file.

The csc.rsp file references all the assemblies shipped with the .NET Framework. The actual references that the Visual Studio .NET development environment includes depend on the project type.

Source

csc.rps (v4.0.30319) 的常规内容:

/r:Accessibility.dll
/r:Microsoft.CSharp.dll
/r:System.Configuration.dll
/r:System.Configuration.Install.dll
/r:System.Core.dll
/r:System.Data.dll
/r:System.Data.DataSetExtensions.dll
/r:System.Data.Linq.dll
/r:System.Data.OracleClient.dll
/r:System.Deployment.dll
/r:System.Design.dll
/r:System.DirectoryServices.dll
/r:System.dll
/r:System.Drawing.Design.dll
/r:System.Drawing.dll
/r:System.EnterpriseServices.dll
/r:System.Management.dll
/r:System.Messaging.dll
/r:System.Runtime.Remoting.dll
/r:System.Runtime.Serialization.dll
/r:System.Runtime.Serialization.Formatters.Soap.dll
/r:System.Security.dll
/r:System.ServiceModel.dll
/r:System.ServiceModel.Web.dll
/r:System.ServiceProcess.dll
/r:System.Transactions.dll
/r:System.Web.dll
/r:System.Web.Extensions.Design.dll
/r:System.Web.Extensions.dll
/r:System.Web.Mobile.dll
/r:System.Web.RegularExpressions.dll
/r:System.Web.Services.dll
/r:System.Windows.Forms.Dll
/r:System.Workflow.Activities.dll
/r:System.Workflow.ComponentModel.dll
/r:System.Workflow.Runtime.dll
/r:System.Xml.dll
/r:System.Xml.Linq.dll