CA2204 warning for mentioning type name in string literal

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?


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.


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, ie 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.

链接地址: http://www.djcxy.com/p/69044.html

上一篇: 代码分析在本地警告,但在TFS构建时出错

下一篇: CA2204警告提及字符串文字中的类型名称