.NET 7 Preview 2 is released, and the second preview includes enhancements to the RegEx source generator, progress in moving NativeAOT from experimental state to runtime, and a series of major improvements to the “dotnet new” CLI SDK.

Download .NET 7 Preview 2 for Windows, macOS, and Linux here.

Introduces a new regular expression source generator

The new regular expression source generator (Issues 44676) provides performance benefits for compilation without increasing startup costs, as well as a good debugging experience.

To get started with the new regular expression source generator, simply convert the contained type to a partial type and declare a new partial method using the RegexGenerator property. The method returns an optimized Regex object, and the source generator automatically populates the method’s implementation and automatically updates when the schema changes or passes other options. Here’s an example:

before:

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

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

Right now:

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 partia

.NET 7 Preview 2 已发布,第二个预览版包括对 RegEx 源生成器的增强、将 NativeAOT 从实验状态转移到运行时的进展,以及对“dotnet new”CLI SDK 的一系列重大改进。

在此下载适用于 Windows、macOS 和 Linux 的 .NET 7 Preview 2 。
引入新的正则表达式源生成器

新的正则表达式源生成器(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.
// ..
}
}

NativeAOT 更新

该版本将 NativeAOT 从实验性的 dotnet/runtimelab 存储库中移出并进入稳定的运行时库 dotnet/runtime repo,但尚未在 dotnet SDK 中添加足够的支持,以使用 NativeAOT 发布项目。
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 Individual
Individual IndividualB2C MultiOrg None SingleOrg Windows

 

该预览版本还有大量其他更新项目,详情请查看更新公告。

发表评论

后才能评论