Warning CA2204 for specifying a type name in a string literal

Consider the following C # code:

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

When creating the project " CA2204 : literals must be spelled correctly" a warning is issued due to the unrecognized word "ContentProperty".

I know that I can disable the rule (either globally, or only for the containing method), or create my own code analysis dictionary and add "ContentProperty" to it as a recognized word. However, none of these solutions seem attractive to me. Referring to the name of the type or class member in the exception message, a lot happens in my project, which is the application infrastructure.

Does Code Analysis have a way of saying that a word / group of words is not intended for spell checking , for example, when they are surrounded by quotation marks or something else? Or disables the warning only along this path?

+10
c # visual-studio code-analysis
source share
4 answers

In Visual Studio 2017 †, I demonstrated that CA2204 code analysis warnings: literals must be written 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 can also find my answer to string interpolation in Visual Studio 2015 and IFormatProvider (CA1305) to avoid CA1305: specify IFormatProvider to be useful.


† Please note that C # version 6 was shipped with Visual Studio 2013. A newer version of Visual Studio (with a newer version of code analysis) may also be required to avoid this warning.

+1
source share

I would use a different approach, since saving a user dictionary can become a maintenance problem: there is no reference to the actual class (in your example ContentPropertyAttribute ). What happens if someone renames or removes this class? Entries in user attributes must be manually synchronized, which is error prone.

Instead, I suggest using a little (I dare say) reflection to introduce the corresponding part of the line (instead of resources that might end up being CA1703 ). Your example can be rewritten as:

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

Now you even have your own time safety for your message.

+3
source share

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

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

The contents of the file should look like this:

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

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

+2
source share

Is there a way in code analysis to say that a word is not intended to check spelling , for example, when it is surrounded by quotation marks or something else?

CA2204 only applies to string literals, i.e. hardcoded strings (surrounded by quotation marks). Disabling this code analysis rule will not prevent the CA from checking spelling for class names, public elements, or other code properties.

If your project is an application infrastructure where most / all string literals will be targeted at developers (e.g. exception messages), I would recommend disabling this rule. For me this makes more sense than coming up with a sophisticated method to exclude every unrecognized line in exception messages.

Another option is to move the error lines to the Resource.resx file. However, you will have the same problem if CA1703 is enabled .

+2
source share

All Articles