What’s new in .NET Productivity/.NET的新特性

The .NET Productivity team (a.k.a. Roslyn) wants to help you be more productive! We’ve seen a lot of excitement in the past few months over our latest features which automate and reduce editing tasks to a single click and help save you time. In this post, I’ll cover some of the latest .NET productivity features available in Visual Studio 2019.

NET生产力团队(又名Roslyn)希望帮助您提高工作效率!在过去的几个月里,我们看到了许多令人兴奋的最新功能,这些功能可以自动化并将编辑任务减少到一次单击,从而帮助您节省时间。在本文中,我将介绍VisualStudio2019中提供的一些最新的.NET生产力特性。

Tooling improvements/模具改进

Starting in .NET 5.0, Roslyn analyzers are included with the .NET SDK. Roslyn analyzers are enabled, by default, for projects that target .NET 5.0 or later. You can enable them on projects that target earlier .NET versions by setting the EnableNETAnalyzers property to true. You can also use the Project Properties to enable/disable .NET analyzers. To access the Project Properties, right-click on a project within Solution Explorer and select Properties. Next, select the Code Analysis tab where you can either select or clear the checkbox to Enable .NET analyzers.

从.NET5.0开始,Roslyn分析器包含在.NETSDK中。默认情况下,针对.NET5.0或更高版本的项目启用Roslyn分析器。通过将EnableNETAnalyzers属性设置为true,可以在针对早期.NET版本的项目上启用它们。您还可以使用项目属性来启用/禁用.NET分析器。要访问项目属性,请右键单击解决方案资源管理器中的项目并选择属性。接下来,选择“代码分析”选项卡,您可以在其中选择或清除复选框以启用.NET分析器。

Another exciting feature is inline parameter name hints that inserts adornments for literals, casted literals, and object instantiations prior to each argument in function calls. In 16.9 Preview 1, we also added inline type hints for variables with inferred types and lambda parameter types. You’ll first need to turn this option on in Tools > Options > Text Editor > C# or Basic > Advanced and select Display inline parameter name hints and Display inline type hints. You can also use the shortcut Alt+F1 to briefly view hints.

另一个令人兴奋的特性是内联参数名提示,它在函数调用中的每个参数之前为文本、强制转换的文本和对象实例化插入修饰。在16.9预览版1中,我们还为具有推断类型和lambda参数类型的变量添加了内联类型提示。首先需要在“工具”>“选项”>“文本编辑器”>“C”或“基本”>“高级”中启用此选项,然后选择“显示内联参数名称提示”和“显示内联类型提示”。您也可以使用快捷键Alt+F1来短暂地查看提示。

You can now extract members from a selected class to a new base class with the new extract base class refactoring. Place your cursor on either the class name or a highlighted member. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Pull member(s) up to new base class or Extract base class. The new Extract Base Class dialog will open where you can specify the name for the base class and location of where it should be placed. You can select the members that you want to transfer to the new base class and choose to make the members abstract by selecting the checkbox in the Make abstract column.

现在,您可以使用新的“提取基类”重构将所选类中的成员提取到新基类中。将光标放在类名或突出显示的成员上。按Ctrl+。以触发快速操作和重构菜单。选择 将成员拉到新基类或提取基类。 新建 提取 基类 类 对话框将打开,您可以在其中指定基类的名称和应放置基类的位置。您可以选择要传输到新基类的成员,并通过选中 生成 抽象 列中的复选框来选择要将成员转换为新基类的成员。

Code cleanup has new configuration options that can apply formatting and file header preferences set in your EditorConfig file across a single file or an entire solution.

“代码清理”有新的配置选项,可以跨单个文件或整个解决方案应用EditorConfig文件中设置的格式和文件头首选项。

Code fixes and refactorings/代码修复和重构

Code fixes and refactorings are the code suggestions the compiler provides through the light bulb and screwdriver icons. To trigger the Quick Actions and Refactorings menu, press (Ctrl+.) or (Alt+Enter). The following list shows the code fixes and refactorings that are new in Visual Studio 2019:

The inline method refactoring helps you replace usages of a static, instance, and extension method within a single statement body with an option to remove the original method declaration. Place your cursor on the usage of the method. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Next select from one of the following options:

Select Inline <QualifiedMethodName> to remove the inline method declaration:

代码修复和重构是编译器通过灯泡和螺丝刀图标提供的代码建议。要触发“快速操作和重构”菜单,请按(Ctrl+)或(Alt+Enter)。以下列表显示了Visual Studio 2019中新增的代码修复和重构:

内联方法重构有助于您用移除原始方法声明的选项替换单个语句体中静态、实例和扩展方法的用法。将光标放在方法的用法上。按Ctrl+。以触发快速操作和重构菜单。下一步从以下选项中选择一个:

选择Inline<QualifiedMethodName>删除内联方法声明:

Select Inline and keep <QualifiedMethodName> to preserve the original method declaration:

The use pattern matching refactoring introduces the new C# 9 pattern combinators. Along with the pattern matching suggestions, such as converting == to use is where applicable, this code fix also suggests the pattern combinators andor and not when matching multiple different patterns and negating. Place your cursor inside the statement. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Use pattern matching.

使用模式匹配重构引入了新的C#9模式组合器。除了模式匹配建议(如converting==to use is)外,此代码修复还建议在匹配多个不同模式并求反时使用模式组合符and、or和not。将光标放在语句内。按Ctrl+。要触发“快速操作和重构”菜单并选择“使用模式匹配”。

The make class abstract refactoring allows you to easily make a class abstract when you’re trying to write an abstract method in a class that isn’t abstract. Place your cursor on the method error. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Make class ‘abstract’.

当您试图在非抽象类中编写抽象方法时,makeclass abstract重构允许您轻松地将类抽象化。将光标放在方法错误上。按Ctrl+。以触发“快速操作和重构”菜单并选择“使类抽象”。

The convert typeof to nameof refactoring allows you to easily convert instances of typeof(<QualifiedType>).Name to nameof(<QualifiedType>) in C# and instances of GetType(<QualifiedType>).Name to NameOf(<QualifiedType>) in Visual Basic. Using nameof instead of the name of the type avoids the reflections involved when retrieving an object. Place your cursor within the typeof(<QualifiedType>).Name. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Convert ’typeof’ to ’nameof’.

转换类型到重构的名称允许您轻松地将类型的实例(<QualifiedType>)。名称转换为C中(<QualifiedType>)的名称和Visual Basic中GetType(<QualifiedType>)的实例。Name到Visual Basic中的(<QualifiedType>)的名称。使用名称而不是类型名称,可以避免检索对象时涉及的反射。将光标放置在(<qualityType>)的类型中。Name。按Ctrl+。要触发QuickActions和Refactorings菜单,请选择 Convert typeof' to nameof'。

Visual Basic had multiple ways of passing parameters, ByVal and ByRef, and for a long time ByVal has been optional. We now fade ByVal to say it’s not necessary along with a code fix to remove the unnecessary ByVal. Place your cursor on the ByVal keyword. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select ‘ByVal’ keyword is unnecessary and can be removed.

visualbasic有多种传递参数的方法,ByVal和ByRef,在很长一段时间内,ByVal是可选的。我们现在淡出ByVal来说明它不需要与代码修复一起删除不必要的ByVal。将光标放在ByVal关键字上。按Ctrl+。触发“快速操作和重构”菜单并选择“ByVal”关键字是不必要的,可以删除。

Now, there’s also a code fix to remove the in keyword where the argument shouldn’t be passed by reference. Place your cursor on the error. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Remove ‘in’ keyword.

现在,还有一个代码修复程序,用于删除不应通过引用传递参数的in关键字。将光标放在错误上。按Ctrl+。以触发“快速操作和重构”菜单并选择“删除”“in”关键字。

In 16.9 Preview 1, we also added a code fix that removes redundant equality expressions for both C# and Visual Basic. Place your cursor on the redundant equality expression. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Remove redundant equality.

在16.9预览版1中,我们还添加了一个代码修复程序,该代码修复程序删除了C#和Visual Basic的冗余相等表达式。将光标放在多余的相等表达式上。按Ctrl+。要触发“快速操作和重构”菜单并选择“删除冗余相等”。

Get involved/参与进来

This was just a sneak peek of what’s new in Visual Studio 2019. For a complete list of what’s new, see the release notes. And feel free to provide feedback on the Developer Community website, or using the Report a Problem tool in Visual Studio. You can also share your feedback with us on GitHub or tweet @roslyn, we’d love to hear what you think!

这只是对visualstudio2019的新功能的一个粗略的了解。有关新增功能的完整列表,请参阅发行说明。您可以在开发人员社区网站上提供反馈,也可以在VisualStudio中使用“报告问题”工具。您也可以在GitHub或tweet@roslyn上与我们分享您的反馈,我们很乐意听到您的想法!

作者:Mika

翻译:刘海鑫

猜你喜欢

转载自blog.csdn.net/u014249305/article/details/109921922