RequireNamedArgs 0.0.7
dotnet add package RequireNamedArgs --version 0.0.7
NuGet\Install-Package RequireNamedArgs -Version 0.0.7
<PackageReference Include="RequireNamedArgs" Version="0.0.7" />
paket add RequireNamedArgs --version 0.0.7
#r "nuget: RequireNamedArgs, 0.0.7"
// Install RequireNamedArgs as a Cake Addin #addin nuget:?package=RequireNamedArgs&version=0.0.7 // Install RequireNamedArgs as a Cake Tool #tool nuget:?package=RequireNamedArgs&version=0.0.7
Require a method to be invoked with named arguments
This package provides a Roslyn code analyzer and an accompanying code-fix provider, that let you force named arguments in C#.
How to use it?
Install this nuget package.
Introduce RequireNamedArgsAttribute
attribute to your solution. In other words, place the following C# code in an appropriate spot in your solution.
[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Struct)]
class RequireNamedArgsAttribute : Attribute {}
Apply the [RequireNamedArgs]
attribute to the methods that should only be called with named arguments. For example:
[RequireNamedArgs]
public static void TellPowerLevel(string name, int powerLevel) {}
// Elsewhere in your code:
// if `TellPowerLevel` method is called with positional arguments,
// the analyzer will emit an error.
TellPowerLevel(name: "Goku", powerLevel: 9001);
Supported method kinds
The analyzer supports requiring named arguments for the following method kinds
- Regular instance and static methods
- Extension methods
- Regular constructors
- Attribute constructors
- Primary constructors
To mark a record's primary constructor, apply [RequireNamedArgs]
to the record itself.
[RequireNamedArgs]
record Character(string Name, int PowerLevel) {}
[RequireNamedArgs]
record struct CharacterStruct(string Name, int PowerLevel) {}
// Elsewhere in your code:
// if the primary constructor of `Character` or `CharacterStruct` is called with positional arguments,
// the analyzer will emit an error.
new Character(Name: "Goku", PowerLevel: 9001);
new CharacterStruct(Name: "Goku", PowerLevel: 9001);
Configuration
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.RequireNamedArgs.severity = error
The possible severity values are:
error
warning
suggestion
silent
none
default
(in case of this analyzer, it's equal toerror
)
Please take a look at the documentation for a detailed description.
Thank you!
- John Koerner for Creating a Code Analyzer using F#
- Dustin Campbell for CSharpEssentials
- Alireza Habibi for CSharpUseNamedArgumentsCodeRefactoringProvider which provided very useful code examples.
- Steve Smith for his article Improve Tests with the Builder Pattern for Test Data.
License
The RequireNamedArgs analyzer and code-fix provider are licensed under the MIT license.
So they can be used freely in commercial applications.
Learn more about Target Frameworks and .NET Standard.
-
- FSharp.Core (>= 4.5.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Added support for C# primary constructors
- Misc minor improvements and clean-ups