Recently, .NET 7 preview version 2 has been officially released, nearly a month has passed since the release of preview version 1. The second preview of .NET 7 includes enhancements to the RegEx source generator, progress in moving NativeAOT from experimental to runtime, and a number of major improvements to the "dotnet new" CLI experience.
Major updates
Introduce new regular expression source generator
The new regular expression source generator (Issues 44676) brings many performance benefits to compilation and a good debugging experience without increasing the startup cost.
To start using the new regular expression source generator, simply convert the containing type to a partial type and declare a new partial method using the RegexGenerator property. This method will return an optimized Regex object, and the source generator will automatically populate the method's implementation and update it when the mode is changed or other options are passed. E.g:
Before:
public class Foo{
public Regex regex = new Regex(@"abc|def", RegexOptions.IgnoreCase);
public bool Bar(string input)
{
bool isMatch = regex.IsMatch(input);
// ..
}}
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 partial method.
// ..
}}
SDK improvements
New CLI parser + tab completion #2191
The new .NET commands provide a more consistent and intuitive interface to many of the subcommands that users already use. Additionally, support for TAB completion for template options and parameters has been substantially updated, providing quick feedback on valid parameters and options as the user types. Here is an example of the new help output:
❯ 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.
new command name
All commands in help output no longer have the -- prefix, which is more in line with user expectations for subcommands in CLI applications. Older versions (--install, etc.) can still be used to prevent breaking user scripts, and deprecation warnings will be added to these commands in the future to encourage migration.
Tab completion
The dotnet CLI has supported tab completion on popular shells like PowerShell, bash, zsh, and fish for some time. However, achieving meaningful completion depends on individual dotnet commands. For .NET 7, the new command learns how to provide tab completion:
- Available template names (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 winformscontrollib --help uninstall
globaljson proto viewimports winformslib -? update
- Template Options (List of Template Options in Web Templates)
❯ 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
- Allowed values for template options (select value on template parameter)
❯ dotnet new blazorserver --auth IndividualIndividual IndividualB2C MultiOrg None SingleOrg Windows
NativeAOT Updates
Moved NativeAOT out of the experimental dotnet/runtimelab repository and into the stable runtime library dotnet/runtime repo, but has not added first-class support in the dotnet SDK to publish projects with NativeAOT.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。