Domanda

Consider the following C# code:

if (atr == null) throw new InvalidOperationException("No ContentProperty attribute found on type.");

When building the project, a "CA2204: Literals should be spelled correctly" warning is issued because of unrecognized word "ContentProperty".

I am aware that I could disable the rule (either globally or for the containing method only) or create a custom Code Analysis dictionary and add "ContentProperty" in it as a recognized word. However, none of these solutions sounds appealing to me. Referring to a type or class member name in an exception message is bound to happen quite a lot in my project, which is an application framework.

Does Code Analysis has a way to tell that a word / group of words isn't meant to be spell-checked, like when surrounded by quotation marks or something? Or is disabling the warning the only way around this?

È stato utile?

Soluzione 4

With Visual Studio 2017 , I have demonstrated that Code Analysis warning CA2204: Literals should be spelled correctly can be avoided by using the following additions to C# version 6:

if (atr == null)
{
    throw new InvalidOperationException(
        $"No {nameof(ContentProperty)} attribute found on type.");
}

You may also find my answer to String Interpolation in Visual Studio 2015 and IFormatProvider (CA1305) for avoiding CA1305: Specify IFormatProvider to be helpful.


† Note that C# version 6 was delivered with Visual Studio 2013. A newer version of Visual Studio (with a newer version of Code Analysis) might also be necessary to avoid this warning.

Altri suggerimenti

I would use a different approach - as maintaining the Custom Dictionary might become a maintenance issue: there's no link to the actual class (in your example the ContentPropertyAttribute). What happens if somebody renames or removes that class? The entries in the Custom Attributes must be synchronized manually which is error-prone.

Instead, I suggest using a bit of (dare I say it) reflection to inject the corresponding part of the string (instead of Resources that might end in having CA1703). Your example might be rewritten as:

throw new InvalidOperationException(string.Format("No {0} found on type.", typeof(ContentPropertyAttribute).Name);

Now you even have compile time safety for your message.

Does Code Analysis have a way to tell that a word isn't meant to be spell-checked, like when surrounded by quotation marks or something?

CA2204 only applies to string literals, i.e. strings that are hard-coded (surrounded by quotation marks). Disabling this code analysis rule will not prevent CA from checking the spelling on your class names, public members, or other code properties.

If your project is an application framework, where most/all string literals will be targeted at developers (like exception messages), I would recommend disabling this rule. To me, that makes more sense than coming up with a complicated method of excluding every unrecognized string in exception messages.

Another option would be to move the "misspelled" strings into a Resource.resx file. However, you'll have the same problem if CA1703 is enabled.

This article describes how to create a custom dictionary for code analysis: http://msdn.microsoft.com/en-us/library/bb514188.aspx

Create a file called CustomDictionary.xml and add it to your project. Set the Build Action property of the file to CodeAnalysisDictionary

The content of the file should look like this:

<Dictionary>
    <Words>
        <Recognized>
            <Word>ContentProperty</Word>
        </Recognized>
    </Words>
</Dictionary>

As suggested by Dr Willy's Apprentice in comments below it might be a good idea to dynamically generate a dictionary based on the framework's architecture.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top