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 ) 其中 returns
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);
}
}
return 是
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
根据我正在关注的 pluralsight 视频,这应该可以正常工作。经过一些搜索后,许多答案都涉及更改项目中使用的 C# 版本,但似乎无法将其转换为在命令提示符下工作。需要改变什么?
查看修改后的code/comments:
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);
}
}
系统上有多个版本的 C# 编译器 (csc.exe)。所以你有
- Microsoft 编译器作为框架的一部分,例如
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
- Microsoft 编译器 (Roslyn) 作为 Visual Studio 的一部分,如
C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Roslyn\csc.exe
当你用
编译时
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc
您正在使用 .NET Framework 附带的 C# 编译器,此编译器最多只支持 C# 5.0 的语言功能。
例如,它无法编译以下使用新的 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 Studio17\Professional\MSBuild.0\Bin\Roslyn\csc.exe hello.cs
参考
Whosebug 用户 Lex Li 在 this post 中对 C# 编译器的版本历史进行了精彩的解释。
步骤 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:\你的文件夹
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\vbc /target:library SendEmail.cs
在这之后你有这个:代码现在应该编译在一个名为的文件中:C:\yourfolder\SendEmail.dll
我创建了以下程序
public class Program
{
static void Main()
{
System.Console.WriteLine("Hello, World!");
}
}
在命令提示符下编译代码时(c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc hello.cs ) 其中 returns
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);
}
}
return 是
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
根据我正在关注的 pluralsight 视频,这应该可以正常工作。经过一些搜索后,许多答案都涉及更改项目中使用的 C# 版本,但似乎无法将其转换为在命令提示符下工作。需要改变什么?
查看修改后的code/comments:
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);
}
}
系统上有多个版本的 C# 编译器 (csc.exe)。所以你有
- Microsoft 编译器作为框架的一部分,例如
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
- Microsoft 编译器 (Roslyn) 作为 Visual Studio 的一部分,如
C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Roslyn\csc.exe
当你用
编译时c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc
您正在使用 .NET Framework 附带的 C# 编译器,此编译器最多只支持 C# 5.0 的语言功能。
例如,它无法编译以下使用新的 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 Studio17\Professional\MSBuild.0\Bin\Roslyn\csc.exe hello.cs
参考
Whosebug 用户 Lex Li 在 this post 中对 C# 编译器的版本历史进行了精彩的解释。
步骤 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:\你的文件夹 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\vbc /target:library SendEmail.cs 在这之后你有这个:代码现在应该编译在一个名为的文件中:C:\yourfolder\SendEmail.dll