如何从命令行运行Roslyn而不是csc.exe?

时间:2015-08-07 10:45:04

标签: c# visual-studio-2015 roslyn .net-4.6

安装VS 2015后,从命令行运行csc.exe会导致此消息显示到控制台:

  

此编译器作为Microsoft(R).NET Framework的一部分提供,   但只支持C#5以外的语言版本,而不再是C#5   最新版本。对于支持较新版本C#的编译器   编程语言,见   http://go.microsoft.com/fwlink/?LinkID=533240

该链接重定向到GitHub上的Roslyn的存储库 那么,是一种运行支持更新版本的编译器的方法" (Roslyn)来自命令行?

2 个答案:

答案 0 :(得分:24)

听起来你的道路基本不合适。如果您打开VS2015的"开发人员命令提示符"你应该在路径的早期$ProgramFiles(x86)$\MSBuild\14.0\bin - 而中的csc.exe Roslyn。

我怀疑您在c:\Windows\Microsoft.NET\Framework\4.0.30319或类似版本中运行版本 - 基本上是传统版本。

答案 1 :(得分:0)

Roslyn 来自命令行('cmd'),Windows 10 场景示例:
(注意:不需要安装 Visual Studio,只需要安装 .NET 核心)

  1. 打开“cmd”并创建文件夹“dotnet-csharp-tools”:

    D:>mkdir "dotnet-csharp-tools"

  2. 导航到文件夹“dotnet-csharp-tools”:

    D:>cd "dotnet-csharp-tools"

  3. 在文件夹“dotnet-csharp-tools”中下载“nuget.exe”最新版本:

    https://www.nuget.org/downloads

  4. 从以下位置检查“Microsoft.CodeDom.Providers.DotNetCompilerPlatform”的最新版本的名称:

    https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/

    例如:'Microsoft.CodeDom.Providers.DotNetCompilerPlatform -Version 3.6.0'

  5. 从“cmd”(打开文件夹“dotnet-csharp-tools”),运行命令:

    D:\dotnet-csharp-tools>nuget 安装 Microsoft.CodeDom.Providers.DotNetCompilerPlatform -Version 3.6.0

  6. 从“cmd”导航到“D:\dotnet-cmd-tools\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0\tools\Roslyn472”(警告:文件夹名称“Roslyn472”可能不同,如果是其他版本)

    D:\dotnet-csharp-tools>cd Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0\tools\Roslyn472

  7. 从“文件资源管理器”中找到“csc.exe”(在当前文件夹“Roslyn472”中)。 复制名为“csc-roslyn.exe”的“csc.exe”(名称可以任意)。

  8. 对于“Windows 10”,打开:

    '编辑系统环境变量' -> '系统变量' -> “路径”->“编辑”->“新建”-> D:\dotnet-csharp-tools\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0\tools\Roslyn472

  9. 关闭并再次打开“cmd”(命令提示符)。 这个'cmd'需要重启,因为'系统环境变量'被编辑了。

  10. 通过运行命令检查 'csc-roslyn' 是否被 'cmd' 识别:

    csc-roslyn

  11. 创建文件夹 'D:\csharp-projects'(文件夹名称可以是任意的) 并在 'D:\csharp-projects' 中创建 C# 源文件,例如:

Vehicle.cs

class Vehicle
{
    private string name = "unknown";
    private int producedYear = -1;

    public Vehicle(string name, int producedYear)
    {
        this.Name = name;
        this.ProducedYear = producedYear;
    }

    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    public int ProducedYear
    {
        get { return this.producedYear; }
        set { this.producedYear = value; }
    }
}

Car.cs

class Car : Vehicle
{
    private string maker = "unknown";

    public Car(string name, int age, string maker)
    : base(name, age)
    {
        this.Maker = maker;
    }

    public string Maker
    {
        get { return this.maker; }
        set { this.maker = value; }
    }

    public override string ToString()
    {
        return $"{this.Name}, {this.ProducedYear}, {this.Maker}";
    }
}

Autoservice.cs

using System;

class Autoservice
{
    public static void Main()
    {
        
        Car car1 = new Car("Ford Taunus", 1971, "Ford");
        Car car2 = new Car("Opel Ascona", 1978, "Opel");
        Car car3 = new Car("Saab 900", 1984, "Saab");

        Console.WriteLine(car1);
        Console.WriteLine(car2);
        Console.WriteLine(car3);
    }
}
  1. 从“cmd”(命令提示符)打开“D:\csharp-projects”并运行命令:

    csc-roslyn /target:exe /out:Autoservice.exe Vehicle.cs Car.cs Autoservice.cs

  2. 从“cmd”运行:

    Autoservice.exe

  3. 结果应该是:

福特 Taunus,1971 年,福特
欧宝 Ascona, 1978, 欧宝
萨博 900, 1984, 萨博