This project contains a Roslyn code analyzer and an accompanying code-fix provider that suggest using named arguments when calling a method having successive parameters of the same type.
Just install the nuget package. The analyzer is going to look for method invocations that can benefit from named arguments across the project.
public static void IntroduceCharacter(string name, string powerLevel) {}
// Elsewhere in your code:
// if `IntroduceCharacter` method is called with positional arguments,
// the analyzer emits a warning, as the the method has two parameters
// of the same type following one another.
IntroduceCharacter(name: "Goku", powerLevel: "Over 9000!");
The analyzer supports suggesting named arguments for the following method kinds
- Regular instance and static methods
- Extension methods
- Regular constructors
- Attribute constructors
- Primary constructors
Install the UseNamedArgs nuget package. For example, run the following command in the NuGet Package Manager Console.
Install-Package UseNamedArgs
This will download all the binaries, and add necessary analyzer references to your project.
Starting in Visual Studio 2019 version 16.3, you can configure the severity of analyzer rules, or diagnostics, in an EditorConfig file, from the light bulb menu, and the error list.
You can add the following to the [*.cs]
section of your .editorconfig.
[*.cs]
dotnet_diagnostic.UseNamedArgs.severity = suggestion
The possible severity values are:
error
warning
suggestion
silent
none
default
(in case of this analyzer, it's equal towarning
)
Please take a look at the documentation for a detailed description.
- John Koerner for Creating a Code Analyzer using F#
- Dustin Campbell for CSharpEssentials
- Alireza Habibi for CSharpUseNamedArgumentsCodeRefactoringProvider which provided very useful code examples.
The analyzer and code-fix provider are licensed under the MIT license.