.NET 7 預覽版 2 正式發布:RegEx 源生成器增強、NativeAOT 更新
3月15号, .NET 7 預覽版 2正式發布,距預覽版 1 發布已過去将近一(yī / yì /yí)個(gè)月的(de)時(shí)間。.NET 7 的(de)第二個(gè)預覽版包括對 RegEx 源生成器的(de)增強、将 NativeAOT 從實驗狀态轉移到(dào)運行時(shí)的(de)進展,以(yǐ)及對“dotnet new”CLI 體驗的(de)一(yī / yì /yí)系列重大(dà)改進。
主要(yào / yāo)更新内容
引入新的(de)正則表達式源生成器
新的(de)正則表達式源生成器(Issues 44676)在(zài)無需增加啓動成本的(de)情況下,爲(wéi / wèi)編譯帶來(lái)了(le/liǎo)許多性能上(shàng)的(de)好處,還提供了(le/liǎo)良好的(de)調試體驗。
要(yào / yāo)開始使用新的(de)正則表達式源生成器,隻需将包含類型轉換爲(wéi / wèi)分部(partial)類型,并使用 RegexGenerator 屬性聲明一(yī / yì /yí)個(gè)新的(de)分部方法。該方法将返回優化的(de) Regex 對象,源生成器将自動填充該方法的(de)實現,并在(zài)更改模式或傳遞其他(tā)選項時(shí)自動更新。例如:
之(zhī)前:
public class Foo{
public Regex regex = new Regex(@"abc|def", RegexOptions.IgnoreCase);
public bool Bar(string input)
{
bool isMatch = regex.IsMatch(input);
// ..
}}
現在(zài):
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 改進
新的(de) CLI 解析器 + 選項卡完成 #2191
.NET 新命令爲(wéi / wèi)用戶已經使用的(de)許多子(zǐ)命令提供了(le/liǎo)更加一(yī / yì /yí)緻和(hé / huò)直觀的(de)界面。此外,對模闆選項和(hé / huò)參數的(de) TAB 補全的(de)支持已得到(dào)大(dà)量更新,在(zài)用戶鍵入時(shí)對有效參數和(hé / huò)選項提供快速反饋。以(yǐ)下是(shì)新的(de)幫助輸出(chū)示例:
❯ 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.
新命令名稱
幫助輸出(chū)中的(de)所有命令不(bù)再具有 -- 前綴,這(zhè)更符合用戶對 CLI 應用程序中子(zǐ)命令的(de)期望。舊版本(--install 等)仍可用于(yú)防止破壞用戶腳本,将來(lái)會在(zài)這(zhè)些命令中添加過時(shí)警告以(yǐ)鼓勵遷移。
Tab 補全
dotnet CLI 在(zài) PowerShell、bash、zsh 和(hé / huò) fish 等流行的(de) shell 上(shàng)支持 tab 補全已經有一(yī / yì /yí)段時(shí)間了(le/liǎo)。然而(ér),實現有意義的(de)補全取決于(yú)單獨的(de) dotnet 命令。對于(yú) .NET 7,新命令學習了(le/liǎo)如何提供 Tab 補全:
可用的(de)模闆名稱(在(zài) 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 模闆中的(de)模闆選項列表)
❯ 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
模闆選項的(de)允許值(選擇模闆參數上(shàng)的(de)選擇值)
❯ dotnet new blazorserver --auth IndividualIndividual IndividualB2C MultiOrg None SingleOrg Windows
NativeAOT 更新
将 NativeAOT 從實驗性 dotnet/runtimelab 存儲庫中移出(chū) 并進入穩定的(de)運行時(shí)庫 dotnet/runtime repo,但尚未在(zài) dotnet SDK 中添加一(yī / yì /yí)流的(de)支持,以(yǐ)使用 NativeAOT 發布項目。