C#vs C#编译器

ras*_*bin 0 c# roslyn

C#6是否意味着"罗斯林"?

C#和C#编译器是一回事吗?有很多关于C#和新C#功能的文章,但是什么是C#?它是C#编译器还是我需要了解其他相关内容?

怎么能看到我的Visual Studio的哪个版本用来编译代码?我知道例如vs 2015默认使用C#6但它如何管理它,任何配置文件或其他什么?

我可以在输出窗口编译代码时看到visualstudio使用的命令吗?(像"cl.exe ... main.c"之类的东西)

每个C#编译器的路径是什么?

svi*_*ick 7

C#6是否意味着"Roslyn"?

实际上,是的.没有正式的规范(希望存在非官方的草案,希望在不久的将来会有所改进).Mono C#编译器确实支持C#6.0,但据我所知,它正在被Roslyn编译器取代.

C#和C#编译器是一回事吗?

理论上,C#语言(由其规范定义,如果存在)和Microsoft开发的C#编译器(又名Roslyn)之间存在差异.在实践中,它们非常紧密地对应在一起.C#6.0与"Roslyn编译器1.0版支持的语言"或"VS 2015支持的C#版本"相同.(这同样适用于C#7.0,Roslyn 2.0和VS 2017.)

怎么能看到我的Visual Studio的哪个版本用来编译代码?

除非你做一些不寻常的事情,比如自己编译Roslyn并在VS的实例中使用它,VS使用与它一起发布的C#编译器版本.这意味着VS 2015的C#6.0和VS 2017的C#7.0.有关完整列表,请参阅维基百科关于C#的文章中的这个表(查看第一列和最后一列).

您还可以使用以前版本的语言编译较新的编译器.要在VS中执行此操作,请转到项目属性→构建→高级→语言版本.

我知道例如vs 2015默认使用C#6但它如何管理它,任何配置文件或其他什么?

Visual Studio的C#扩展包括一个内置的C#编译器,用于IntelliSense之类的东西.

为了实际构建项目,VS使用MSBuild,它使用.targets文件配置.具体来说,如果要设置自己的csc.exe路径,可以使用该CscToolPath属性,例如在命令行上:msbuild /p:CscToolPath=my\csc\path或添加<PropertyGroup><CscToolPath>my\csc\path</CscToolPath></PropertyGroup>到.csproj文件中.

我可以在输出窗口编译代码时看到Visual Studio正在使用的命令吗?

一种方法是从VS的开发人员命令提示符运行msbuild(可能/t:rebuild确保编译器实际运行).

例如,对于在VS 2017中创建的新C#控制台应用程序项目,我的输出是:

Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 27. 3. 2017 16:39:39.
Project "C:\code\tmp\ConsoleApp2\ConsoleApp2.csproj" on node 1 (rebuild target(s)).
CoreClean:
  Deleting file "C:\code\tmp\ConsoleApp2\bin\Debug\ConsoleApp2.exe.config".
  Deleting file "C:\code\tmp\ConsoleApp2\bin\Debug\ConsoleApp2.exe".
  Deleting file "C:\code\tmp\ConsoleApp2\bin\Debug\ConsoleApp2.pdb".
  Deleting file "C:\code\tmp\ConsoleApp2\obj\Debug\ConsoleApp2.csprojResolveAssemblyReferen
  ce.cache".
  Deleting file "C:\code\tmp\ConsoleApp2\obj\Debug\ConsoleApp2.exe".
  Deleting file "C:\code\tmp\ConsoleApp2\obj\Debug\ConsoleApp2.pdb".
GenerateBindingRedirects:
  No suggested binding redirects from ResolveAssemblyReferences.
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-t
o-date with respect to the input files.
CoreCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\Roslyn\cs
  c.exe /noconfig /nowarn:1701,1702 /nostdlib+ /platform:anycpu32bitpreferred /errorreport:
  prompt /warn:4 /define:DEBUG;TRACE /highentropyva+ /reference:"C:\Program Files (x86)\Ref
  erence Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\Microsoft.CSharp.dll" /referen
  ce:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\
  mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework
  \.NETFramework\v4.6.2\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assem
  blies\Microsoft\Framework\.NETFramework\v4.6.2\System.Data.DataSetExtensions.dll" /refere
  nce:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2
  \System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Frame
  work\.NETFramework\v4.6.2\System.dll" /reference:"C:\Program Files (x86)\Reference Assemb
  lies\Microsoft\Framework\.NETFramework\v4.6.2\System.Net.Http.dll" /reference:"C:\Program
   Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\System.Xml.dll
  " /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramewo
  rk\v4.6.2\System.Xml.Linq.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\Deb
  ug\ConsoleApp2.exe /ruleset:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterpri
  se\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset" /subsyste
  mversion:6.00 /target:exe /utf8output Program.cs Properties\AssemblyInfo.cs "C:\Users\Svi
  ck\AppData\Local\Temp\.NETFramework,Version=v4.6.2.AssemblyAttributes.cs"
  Using shared compilation with compiler from directory: C:\Program Files (x86)\Microsoft V
  isual Studio\2017\Enterprise\MSBuild\15.0\bin\Roslyn
_CopyAppConfigFile:
  Copying file from "App.config" to "bin\Debug\ConsoleApp2.exe.config".
CopyFilesToOutputDirectory:
  Copying file from "obj\Debug\ConsoleApp2.exe" to "bin\Debug\ConsoleApp2.exe".
  ConsoleApp2 -> C:\code\tmp\ConsoleApp2\bin\Debug\ConsoleApp2.exe
  Copying file from "obj\Debug\ConsoleApp2.pdb" to "bin\Debug\ConsoleApp2.pdb".
Done Building Project "C:\code\tmp\ConsoleApp2\ConsoleApp2.csproj" (rebuild target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.33
Run Code Online (Sandbox Code Playgroud)

请注意该CoreCompile部分,其中显示了用于编译项目的命令.

获得相同输出的另一种方法是将VS中的MSBuild详细程度更改为Normal,Build(或Rebuild),然后查看Output窗口.

每个C#编译器的路径是什么?

您可以通过打开该版本的VS并运行的开发人员命令提示符,找出与某个版本的VS对应的csc.exe的位置where csc.对我来说,这C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe为VS 2015 Update 3 打印(版本为C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Roslyn\csc.exe1.3.1.60616),为VS 2017 打印(版本2.0.0.61501).

路径不同的原因是因为VS 2017在其各种组件的安装位置发生了变化.

在这两种情况下,C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe还会打印第二行,这是旧的Roslyn C#5.0编译器(版本4.6.1586.0).