CA2204 warning for mentioning type name in string literal
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.