简体   繁体   English

在 asp.net 应用程序中启用 c# 7

[英]Enabling c# 7 in a asp.net application

I just started working on my old solution in Visual Studio 2017. Just opening the solution in the old IDE worked seamlessly.我刚刚开始在 Visual Studio 2017 中处理我的旧解决方案。只需在旧 IDE 中打开该解决方案即可无缝工作。 The c# application projects now default to the c# 7.0 compiler. c# 应用程序项目现在默认使用 c# 7.0 编译器。 The property pages of those project (compilation/advanced) let easily chose the targeted language version of the compiler, defaulting to the latest.这些项目的属性页(编译/高级)可以轻松选择编译器的目标语言版本,默认为最新。

I cannot find a way to enable c# 7.0 in the asp.net web projects though.虽然我找不到在asp.net web 项目中启用 c# 7.0 的方法。 If I write a statement such as:如果我写一个声明,例如:

if (int.TryParse("1", out int myInt)) { ... }

the IDE warns me saying that I need to use version 7+ of the language. IDE 警告我说我需要使用该语言的 7+ 版本。

My research on this topic shows I should target the specific c# version in the system.codedom compilers area of the web.config file, so to target the newest Roslyn version.我对这个主题的研究表明,我应该以 web.config 文件的 system.codedom 编译器区域中的特定 c# 版本为目标,以便以最新的 Roslyn 版本为目标。

What I have now is:我现在拥有的是:

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>

which targets c# 6. What are the correct settings for c# 7, provided that I have already downloaded the latest Roslyn with nuget?哪个针对 c# 6。如果我已经用 nuget 下载了最新的 Roslyn,那么 c# 7 的正确设置是什么?

Update Here is a screenshot of the available Compile options for a web project (it is Italian VS2017 but it should be easy to understand).更新这是一个 web 项目可用编译选项的屏幕截图(它是意大利语 VS2017,但应该很容易理解)。 No possibility to select the targeted c# version there.无法在那里选择目标 c# 版本。

编译选项

In website's NuGet window:在网站的 NuGet 窗口中:

  1. Uninstall Microsoft.CodeDom.Providers.DotNetCompilerPlatform卸载Microsoft.CodeDom.Providers.DotNetCompilerPlatform
  2. Re-install it重新安装
  3. In web.config , under: system.codedom > compilers > compiler , change compilerOptions ="/langversion: 6 to 7web.config下: system.codedom > compilers > compiler ,将compilerOptions ="/langversion: 6 to 7

I am able to compile it with default language setting but not with C# 7 option.我可以使用默认语言设置编译它,但不能使用 C# 7 选项编译它。 在此处输入图片说明

But below setting gives compile time error:但下面的设置给出了编译时错误:

在此处输入图片说明

so you can keep your language version setting as default.因此您可以将语言版本设置保留为默认值。

If you experimenting with Roslyn and not using Visual 2017 default compiler build then you may need to make some more changes如果您尝试使用 Roslyn 而不是使用 Visual 2017 默认编译器构建,那么您可能需要进行更多更改

Select your project name and right click >> Properties Window >> Build and then add the below two options in "Conditional Compilation symbols" text box __DEMO__,__DEMO_EXPERIMENTAL__选择您的项目名称并右键单击>>属性窗口>>构建然后在“条件编译符号”文本框中添加以下两个选项__DEMO__,__DEMO_EXPERIMENTAL__

在此处输入图片说明

Update更新

In order to use C# 7.0, you need to use 2.0+ version of Microsoft.Net.Compilers为了使用 C# 7.0,您需要使用 2.0+ 版本的 Microsoft.Net.Compilers

在此处输入图片说明

after installing the latest version of Microsoft.Net.Compilers (2.0+) you can select the language version as C# 7.安装最新版本的 Microsoft.Net.Compilers (2.0+) 后,您可以选择语言版本为 C# 7。

so the best solution is to install the latest version of Microsoft.Net.Compilers (2.0+).所以最好的解决方案是安装最新版本的 Microsoft.Net.Compilers (2.0+)。

For C# 7.x support set the Build Configuration Language Version of project to C# latest minor version (latest)对于 C# 7.x 支持,将项目的构建配置语言版本设置为 C# 最新次要版本(最新)

Build Configuration Language Version构建配置语言版本

If you are using CodeDOM Providers for .NET Compiler Platform ("Roslyn") (eg the Microsoft.CodeDom.Providers.DotNetCompilerPlatform nuget package) set compilerOptions="/langversion:latest" in web.config for asp.net.如果您使用 CodeDOM Providers for .NET Compiler Platform ("Roslyn")(例如 Microsoft.CodeDom.Providers.DotNetCompilerPlatform nuget 包),请在 web.config 中为 asp.net 设置 compilerOptions="/langversion:latest"。

<system.codedom>
   <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:latest /nowarn:1659;1699;1701"/>
   </compilers>
</system.codedom>

For more info:欲了解更多信息:

If you try to install Microsoft.CodeDom.Providers.DotNetCompilerPlatform version 2.0.0 and your project targets a version of .net older than 4.6, then it will automatically use an older version of roslyn which only supports up to langversion 6. This is because newer versions of roslyn, including the first version to support csharp-7, require at least .net-4.6 to run.如果您尝试安装Microsoft.CodeDom.Providers.DotNetCompilerPlatform版本 2.0.0 并且您的项目针对 .net 版本早于 4.6,那么它将自动使用仅支持 langversion 6 的旧版本 roslyn。这是因为较新版本的 roslyn,包括第一个支持 csharp-7 的版本,至少需要 .net-4.6 才能运行。 If your project targets an older version of .net, you will get the error message you see:如果您的项目面向较旧版本的 .net,您将收到以下错误消息:

CS1617 Invalid option 'latest' for /langversion; CS1617 /langversion 的无效选项“最新”; must be ISO-1, ISO-2, Default or an integer in range 1 to 6.必须是 ISO-1、ISO-2、默认值或 1 到 6 范围内的整数。

  1. Ensure that your project is targeting at least .net-4.6.确保您的项目至少针对 .net-4.6。 Retarget if necessary.必要时重新定位。
  2. If your project still uses packages.config , then you must uninstall and reinstall Microsoft.CodeDom.Providers.DotNetCompilerPlatform to update your project file to point to the .net-4.6 variant of the nuget package.如果您的项目仍然使用packages.config ,那么您必须卸载并重新安装Microsoft.CodeDom.Providers.DotNetCompilerPlatform以更新您的项目文件以指向 nuget 包的 .net-4.6 变体。 If you are using <PackageReference/> , you are all set (but you have to manually configure web.config 's system.codedom section).如果您使用的是<PackageReference/> ,那么您已经设置好了(但您必须手动配置web.configsystem.codedom部分)。

You need to replace files in your project folder您需要替换项​​目文件夹中的文件

/Bin/roslyn 

with files from NuGet package folder使用 NuGet 包文件夹中的文件

/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0/tools/Roslyn472

csv.exe in project folder not replaced during installation a new version of nuget.安装新版本 nuget 期间未替换项目文件夹中的 csv.exe。 After replacing files it works excellent.替换文件后效果很好。

And do not forget to change .Net version to latest in project properties.并且不要忘记将 .Net 版本更改为项目属性中的最新版本。

我正在引用一个引用另一个自定义项目 B 的自定义项目 A。我刚刚阅读了从 A 到 B 的引用,它似乎工作(现在)。

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

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