.NET 7 预览版 2 正式发布:RegEx 源生成器增强、NativeAOT 更新
3月15号, .NET 7 预览版 2正式发布,距预览版 1 发布已过去将近一个月的时间。.NET 7 的第二个预览版包括对 RegEx 源生成器的增强、将 NativeAOT 从实验状态转移到运行时的进展,以及对“dotnet new”CLI 体验的一系列重大改进。
主要更新内容
引入新的正则表达式源生成器
新的正则表达式源生成器(Issues 44676)在无需增加启动成本的情况下,为编译带来了许多性能上的好处,还提供了良好的调试体验。
要开始使用新的正则表达式源生成器,只需将包含类型转换为分部(partial)类型,并使用 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 改进
新的 CLI 解析器 + 选项卡完成 #2191
.NET 新命令为用户已经使用的许多子命令提供了更加一致和直观的界面。此外,对模板选项和参数的 TAB 补全的支持已得到大量更新,在用户键入时对有效参数和选项提供快速反馈。以下是新的帮助输出示例:
❯ 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 补全已经有一段时间了。然而,实现有意义的补全取决于单独的 dotnet 命令。对于 .NET 7,新命令学习了如何提供 Tab 补全:
可用的模板名称(在 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        winformscontrollib   --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 IndividualIndividual     IndividualB2C  MultiOrg       None           SingleOrg      Windows
NativeAOT 更新
将 NativeAOT 从实验性 dotnet/runtimelab 存储库中移出 并进入稳定的运行时库 dotnet/runtime repo,但尚未在 dotnet SDK 中添加一流的支持,以使用 NativeAOT 发布项目。