官宣 .NET 7 Preview 2

786 阅读6分钟

今天,我们很高兴发布 .NET 7 预览版 2。.NET 7 的第二个预览版包括对 RegEx 源生成器的增强、将 NativeAOT 从实验状态转移到运行时的进展,以及对"dotnet new"CLI 的一系列重大改进经验。这些可供您立即获取_并_开始尝试新功能,例如:

  • 在编译时使用源生成器而不是在运行时使用较慢的方法来构建专门的 RegEx 模式匹配引擎。
  • dotnet new利用 SDK 改进提供全新的简化选项卡完成体验来探索模板和参数。
  • 不要削减用你自己的创新解决方案尝试 NativeAOT。

EF7 预览版 2 也已发布,可在NuGet 上使用。您还可以阅读ASP.NET Core Preview 2 中的新增功能

您可以下载适用于 Windows、macOS 和 Linux 的.NET 7 Preview 2 。

如果您想在 Visual Studio 系列产品中试用 .NET 7,我们建议您使用预览频道版本。Visual Studio for Mac 对 .NET 7 预览版的支持尚不可用,但即将推出。

预览版 2

Preview 2 版本现在提供以下功能。

引入新的正则表达式源生成器

github.com/dotnet/runt…

您是否曾经希望拥有针对您的特定模式优化的专用正则表达式引擎所带来的所有巨大好处,而无需在运行时构建该引擎的开销?

我们很高兴地宣布包含在预览版 1 中的新正则表达式源生成器。它带来了我们编译引擎的所有性能优势,而无需启动成本,并且它具有其他优势,例如提供出色的调试体验以及修剪-友好的。如果您的模式在编译时是已知的,那么新的正则表达式源生成器就是要走的路。

为了开始使用它,您只需要将包含类型转换为部分类型并使用RegexGenerator属性声明一个新的部分方法,该方法将返回优化的Regex对象,就是这样!源代码生成器将为您填充该方法的实现,并在您更改模式或传入的其他选项时自动更新。这是一个示例:

public class Foo
{
  public Regex regex = new Regex(@"abc|def", RegexOptions.IgnoreCase);

  public bool Bar(string input)
  {
    bool isMatch = regex.IsMatch(input);
    // ..
  }
}

public partial class Foo  // <-- Make the class a partial class
{
  [RegexGenerator(@"abc|def", RegexOptions.IgnoreCase)] // <-- Add the RegexGenerator attribute and pass in your pattern and options
  public static partial Regex MyRegex(); //  <-- Declare the partial method, which will be implemented by the source generator

  public bool Bar(string input)
  {
    bool isMatch = MyRegex().IsMatch(input); // <-- Use the generated engine by invoking the partial method.
    // ..
  }
}

就是这样。请尝试一下,如果您有任何反馈,请告诉我们。

SDK 改进

[Epic] 新的 CLI 解析器 + 选项卡完成 #2191

对于**7.0.100-preview2 **, dotnet new命令为用户已经使用的许多子命令提供了更加一致和直观的界面。此外,对模板选项和参数的制表符完成的支持已得到大量更新,现在可以在用户键入时对有效参数和选项提供快速反馈。

以下是新的帮助输出示例:

❯ dotnet new --help
Description:
  Template Instantiation Commands for .NET CLI.

Usage:
  dotnet new [<template-short-name> [<template-args>...]] [options]
  dotnet new [command] [options]

Arguments:
  <template-short-name>  A short name of the template to create.
  <template-args>        Template specific options to use.

Options:
  -?, -h, --help  Show command line help.

Commands:
  install <package>       Installs a template package.
  uninstall <package>     Uninstalls a template package.
  update                  Checks the currently installed template packages for update, and install the updates.
  search <template-name>  Searches for the templates on NuGet.org.
  list <template-name>    Lists templates containing the specified template name. If no name is specified, lists all templates.

新命令名称

具体来说,此帮助输出中的所有_命令_不再像现在那样具有--前缀。这更符合用户对 CLI 应用程序中子命令的期望。旧版本( --install等)仍可用于防止破坏用户脚本,但我们希望将来在这些命令中添加过时警告以鼓励迁移。

Tab自动补全

dotnet CLI 在 PowerShell、bash、zsh 和 fish 等流行的 shell 上支持 tab 补全已经有一段时间了(有关如何启用它的说明,请参阅如何为.NET CLI 启用TAB 补全)。然而,实现有意义的补全取决于单独的dotnet命令。对于 .NET 7,new命令学习了如何提供Tab自动补全

  • 可用的模板名称(in dotnet new < template-short-name > )
❯ dotnet new angular
angular              grpc            razor            viewstart       worker               -h
blazorserver         mstest              razorclasslib        web                  wpf         /?      
blazorwasm           mvc    razorcomponent       webapi               wpfcustomcontrollib  /h
classlib             nugetconfig         react         webapp           wpflib     install          
console              nunit      reactredux           webconfig      wpfusercontrollib    list
editorconfig         nunit-test           sln       winforms             xunit         search       
gitignore            page         tool-manifest      wnformscontrollib   --help     uninstall       
globaljson           proto        viewimports          winformslib          -?      update             

  • 模板选项(Web模板中的模板选项列表)
❯ dotnet new web --dry-run
--dry-run                  --language                 --output                   -lang
--exclude-launch-settings  --name                     --type                     -n
--force                    --no-https                 -?                         -o
--framework                --no-restore               -f                         /?
--help                     --no-update-check          -h                         /h
  • 这些模板选项的允许值(选择模板参数上的选择值)
❯ dotnet new blazorserver --auth Individual
Individual     IndividualB2C  MultiOrg       None           SingleOrg      Windows

当然也有一些已知的差距——例如,-- language不建议安装语言值。

未来的工作

在未来的预览版中,我们计划继续填补这一过渡留下的空白,并让自动完成或像用户可以执行的单个命令一样简单。我们希望这将改进整个dotnet CLI 的Tab补全功能,并被社区更广泛地使用!

下一步是什么

dotnet new users – 启用Tab补全并尝试使用模板!模板作者 – 在您的模板上尝试Tab补全,并确保您提供您希望您的用户拥有的体验。大家 - 提出您在dotnet/templating存储库中发现的任何问题,并帮助我们使.NET 7 成为dotnet new的最佳版本!

NativeAOT 更新

我们之前宣布,我们正在将NativeAOT 项目从实验状态转移到 .NET 7 的主线开发中。在过去的几个月里,我们一直在埋头进行编码,以将 NativeAOT 从实验性dotnet/runtimelab repo中移出并进入dotnet/runtimerepo。

该工作现已完成,但我们尚未在 dotnet SDK 中添加支持,来使用 NativeAOT 发布项目。我们希望尽快完成这项工作,以便您可以在您的应用程序中试用 NativeAOT。同时,请尝试修剪您的应用并确保没有修剪警告。修剪是 NativeAOT 的要求。如果您拥有任何库,请参考准备进行修剪库的说明

Targeting .NET 7

要targeting .NET 7,您需要在项目文件中使用 .NET 7 Target Framework Moniker (TFM)。例如:

<TargetFramework> net7.0 </TargetFramework>

全套 .NET 7 TFM,包括特定于操作系统的TFM。

  • net7.0
  • net7.0-android
  • net7.0-ios
  • net7.0-maccatalyst
  • net7.0-macos
  • net7.0-tvos
  • net7.0-windows

我们希望从 .NET 6 升级到 .NET 7 应该很简单。请报告您在使用 .NET 7 测试现有应用程序的过程中发现的任何重大更改。

支持

.NET 7 是 当前 版本,这意味着它将在发布之日起 18 个月内获得免费支持和补丁。请务必注意,所有版本的质量都是相同的。唯一的区别是支持的时间。有关 .NET 支持政策的更多信息,请参阅.NET 和.NET Core 官方支持政策

重大变化

您可以通过阅读 .NET 7 中的重大更改文档找到最新的.NET 7 重大更改列表。它按区域和版本列出了重大更改,并附有详细说明的链接。

要查看提出了哪些重大更改但仍在审核中,请关注Proposed .NET Breaking Changes GitHub 问题

路线图

.NET 版本包括产品、库、运行时和工具,代表了 Microsoft 内外多个团队之间的协作。您可以通过阅读产品路线图了解有关这些领域的更多信息:

结束

我们感谢您对 .NET 的所有支持和贡献。请尝试 .NET 7 Preview 2 并告诉我们您的想法!