繁体   English   中英

VS 2017开发人员命令提示中的语言版本

[英]Language version in Developer Command Prompt for VS 2017

我创建了以下程序

public class Program
{
    static void Main()
    {
        System.Console.WriteLine("Hello, World!");
    }
}

在命令提示符( c:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0.30319 \\ csc hello.cs )中编译代码时,该代码将返回

Microsoft (R) Visual C# Compiler version 4.7.3056.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

该程序仍在编译,所以我认为没什么大不了的。 根据我完成的其他搜索,似乎此消息仅供参考。

但是,对于程序

public class Program
{
    static void Main()
    {
        System.Console.WriteLine(DateTime.Now.DayOfWeek);
    }
}

回报是

Microsoft (R) Visual C# Compiler version 4.7.3056.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

hello.cs(5,28): error CS0103: The name 'DateTime' does not exist in the current context

根据我跟随的复数视频,它应该可以正常工作。 经过一些搜索后,许多答案都涉及更改项目中使用的C#版本,但似乎无法将其转换为在命令提示符下工作。 需要改变什么?

系统上有几种版本的C#编译器(csc.exe)。 所以你有了

  1. 作为框架一部分的Microsoft编译器,例如c:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe
  2. 作为Visual Studio一部分的Microsoft编译器(Roslyn),例如C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\15.0\\Bin\\Roslyn\\csc.exe

当您编译时

c:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0.30319 \\ csc

您正在使用.NET Framework附带的C#编译器,并且该编译器仅支持不超过C#5.0的语言功能。

例如,它不能编译Await in catch/finally blocks使用新的C#6.0语言功能Await in catch/finally blocks的以下程序。

class Program
{
    static void Main(string[] args)
    {
    }

    async Task<int> GetVAsync()
    {
        try
        {
        }
        catch
        {
            //gives error CS1985: Cannot await in the body of a catch clause
            await Task.Delay(1000); 
        }            
        return 3;
    }
}

因此,警告消息表示

选择使用的编译器(.NET Framework附带的编译器)无法编译C#5.0之后引入的新功能。 如果不使用任何新功能,则编译可能会成功,就像您的示例一样。 但是您最好使用更新的编译器(Visual Studio附带的Roslyn)来充分利用编译器的功能。

解决方案

在命令行中使用它(路径可能因VS版本而异)

C:\\ Program Files(x86)\\ Microsoft Visual Studio \\ 2017 \\ Professional \\ MSBuild \\ 15.0 \\ Bin \\ Roslyn \\ csc.exe hello.cs

参考

计算器用户莱克斯李介绍了在C#编译器的版本历史上的一个极好的说明这篇文章

检查修改后的代码/注释:

using System; //Add this to your code, it should work well

public class Program
{
    static void Main()
    {
        System.Console.WriteLine("Hello, World!");
        System.Console.WriteLine(DateTime.Now.DayOfWeek);
    }
}

步骤1-CLR代码:我们要做的第一件事是编写CLR代码。 可以用C#.NET编写。 在此示例中,我们使用C#。

[Microsoft.SqlServer.Server.SqlProcedure()]
public static void spSendMail(string recipients, string subject, string fromto, string body)
{

    MailMessage message = new MailMessage(fromto, recipients);
    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
    string msg = string.Empty;
    try
    {
        message.From = new MailAddress(fromto);
        message.To.Add(recipients);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        smtpClient.Port = 587;
        smtpClient.Credentials = new System.Net.NetworkCredential(fromto, "779957082");
        smtpClient.EnableSsl = true;
        smtpClient.Send(message);
        msg = "Message ENvoyé !";
        Console.WriteLine(msg);
        //Console.ReadKey();
    }
    catch (Exception ex)
    {
        msg = ex.Message;
        Console.WriteLine(msg);
        //Console.ReadKey();
    }

}

第2步-编译CLR代码为了使用此代码,必须对代码进行编译。

从命令行运行以下命令,以使用csc.exe应用程序编译CLR代码。因此,从命令行运行命令,如下所示:cd C:\\ yourfolder C:\\ Windows \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ vbc / target:library SendEmail.cs在此之后,您将具有:现在应将代码编译在一个名为C:\\ yourfolder \\ SendEmail.dll的文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM