Analyzer that ensures that dictionary with enum key type sets values for all defined enum keys when decorated with a [Exhaustive] attribute
$ dotnet add package ExhaustiveDictionary.AnalyzerWhen defining a dictionary mapping between an enum and another type, place an [Exhaustive] attribute on the dictionary 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.
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
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
Attribute was placed on invalid object, it can only be used on a Dictionary where the key is an Enum.