ExhaustiveDictionary.Analyzer
1.0.6
dotnet add package ExhaustiveDictionary.Analyzer --version 1.0.6
NuGet\Install-Package ExhaustiveDictionary.Analyzer -Version 1.0.6
<PackageReference Include="ExhaustiveDictionary.Analyzer" Version="1.0.6"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="ExhaustiveDictionary.Analyzer" Version="1.0.6" />
<PackageReference Include="ExhaustiveDictionary.Analyzer"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add ExhaustiveDictionary.Analyzer --version 1.0.6
#r "nuget: ExhaustiveDictionary.Analyzer, 1.0.6"
#:package ExhaustiveDictionary.Analyzer@1.0.6
#addin nuget:?package=ExhaustiveDictionary.Analyzer&version=1.0.6
#tool nuget:?package=ExhaustiveDictionary.Analyzer&version=1.0.6
Exhaustive Dictionary Analyzer
When defining a dictionary mapping between an enum and another type, place an [Exhaustive] attribute on the dictionary type to ensure that it defines all of the values of the enum. So when in the future you add a new value, it will become a compile time error if you forgot to add it to the mapping.
Rules:
EXHAUSTIVEDICT0001
Ensures all enum values are defined in dictionaries with an enum type as the key, when attributed with the [Exhaustive] attribute
enum Color { Red, Green, Blue };
[Exhaustive]
Dictionary<Color, string> ColorToHex = new() {
{ Color.Red, "#FF0000" }
};
Here the rule will complain because we have forgotten to add a value for Color.Green and Color.Blue to the dictionary
EXHAUSTIVEDICT0002
Ensure only one value per enum key is defined in the dictionary
enum Color { Red, Green, Blue };
[Exhaustive]
Dictionary<Color, string> ColorToHex = new() {
{ Color.Red, "#FF0000" },
{ Color.Green, "#008000" },
{ Color.Blue, "#0000FF" },
{ Color.Red, "#FF0000" },
};
Here the rule will complain because we have added Color.Red twice. For regular Dictionaries this is not a problem since you would also get a DuplicateKey exception when starting the project. But some dictionary types like FrozenDictionary doesn't throw but overwrite the same key with the last value defined 1
EXHAUSTIVEDICT0003
Attribute was placed on invalid object, it can only be used on a dictionary type (Dictionary, IReadOnlyDictionary, ImmutableDictionary, FrozenDictionary) where the key is an Enum.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.