正式发布 | .NET 7 预览版 2

点击上方蓝字
关注我们
(本文阅读所需15分钟)
今天 , 我们很高兴发布 .NET 7 预览版 2 。 .NET 7 的第二个预览版包括对 RegEx 源生成器的增强、将 NativeAOT 从实验状态转移到运行时的进展 , 以及对“dotnet new”CLI 的一系列重大改进经验 。 这些可供您立即获取并开始尝试新功能 , 例如:

  • 在编译时使用源生成器而不是在运行时使用较慢的方法来构建专门的 RegEx 模式匹配引擎 。
  • dotnet new 利用 SDK 改进提供全新的简化选项卡完成体验来探索模板和参数 。
  • 用你自己的创新解决方案尝试 NativeAOT 。
预览版2
▌ 引入新的正则表达式源生成器
新正则表达式源生成器带来了我们编译引擎的更多性能优势 , 而无需额外成本 , 它还可以提供出色的调试体验以及便于修剪 。 如果您的模式在编译时是已知的 , 那么新的正则表达式源生成器可以很好地帮到您 。
您只需要将包含类型转换为部分类型 , 并使用 RegexGenerator 属性声明一个新的部分方法 , 该方法将返回优化的 Regex 对象 。 源代码生成器将为您填充该方法的实现 , 并在您更改模式或传入的其他选项时自动更新 。
更新前
public class Foo { public Regex regex = new Regex(@"abc|def", RegexOptions.IgnoreCase); public bool Bar(string input){bool isMatch = regex.IsMatch(input);// ..}}
更新后
publicpartialclassFoo// <-- Make the class a partial class{[ RegexGenerator(@ "abc|def", RegexOptions.IgnoreCase) ] // <-- Add the RegexGenerator attribute and pass in your pattern and optionspublicstaticpartialRegex MyRegex( ) ; // <-- Declare the partial method, which will be implemented by the source generatorpublicboolBar( stringinput ) {boolisMatch = MyRegex.IsMatch(input); // <-- Use the generated engine by invoking the partial method.// ..}}
▌SDK 改进
  • [Epic] 新的 CLI 解析器 + 选项卡完成 #2191
对于 7.0.100-preview 2 , dotnet new 命令为用户已经使用的许多子命令提供了更加一致和直观的界面 。 此外 , 对模板选项和参数的制表符完成的支持已得到大量更新 , 现在可以在用户键入时对有效参数和选项提供快速反馈 。
以下是新的帮助输出示例:
? dotnet new--help Deion:Template Instantiation Commands for.NET CLI. Usage:dotnet new[< template- short-name> [< template-args>...]] [options] dotnet new[command] [options]
Arguments:< template- short-name> A shortname of the templateto create. < template-args> Template specific options to use.
Options:-?, -h, --help Show command line help.
Commands:install <package> Installs a templatepackage. uninstall <package> Uninstalls a templatepackage. update Checks the currently installed templatepackages forupdate, andinstall the updates. search < template-name> Searches forthe templates on NuGet.org. list< template-name> Lists templates containing the specified templatename. If no name is specified, lists all templates.
  • 新命令名称
具体来说 , 此帮助输出中的所有命令不再像现在那样具有--前缀 。 这更符合用户对 CLI 应用程序中子命令的期望 。 旧版本( --install等)仍可用于防止破坏用户脚本 , 但我们希望将来在这些命令中添加过时警告以鼓励迁移 。
  • Tab自动补全

    特别声明:本站内容均来自网友提供或互联网,仅供参考,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。