-->

How to get the FxCop custom dictionary to work?

2019-01-23 00:34发布

问题:

How is it possible to get the FxCop custom dictionary to work correctly?

I have tried adding words to be recognised to the file 'CustomDictionary.xml', which is kept in the same folder as the FxCop project file. This does not seem to work, as I still get the 'Identifiers should be spelled correctly' FxCop message, even after reloading and re-running FxCop. Using version 1.36.

回答1:

If you use it inside Visual Studio...

From Visual Studio Code Analysis Team Blog

To add a custom dictionary to a C# and Visual Basic project is simple:

  1. In Solution Explorer, right-click on the project and choose Add -> New Item...
  2. Under Templates, select XML File, enter a name for the dictionary, such as CodeAnalysisDictionary.xml and click Add
  3. In Solution Explorer, right-click on the XML file and choose Properties
  4. In the Properties tool window, under Build Action choose CodeAnalysisDictionary
  5. In Solution Explorer, double-click on the newly created dictionary to open it
  6. In the XML editor, paste the following, replacing "productname" and "companyname" with your team's equivalents:

    <Dictionary>
         <Words>
            <Recognized>
                <Word>"productname"</Word>
                <Word>"companyname"</Word>
            </Recognized>
        </Words>
    </Dictionary>
    

You are now ready to start entering your own custom words. Simply add a new element for each word in your project that does not exist in the dictionary. Each word is case-insensitive, so any casing of the word will be recognized. Code Analysis will automatically pick up the custom dictionary the next time it is run.



回答2:

The easiest way is to just call it "CustomDictionary.xml" and put it in your solution folder, where FxCop (1.36 tested here) will pick it up automatically, if you have

<CustomDictionaries SearchFxCopDir="True"
                    SearchUserProfile="True"
                    SearchProjectDir="True" />

in your FxCop project file.

Alternatively you can specify it via the /dictionary command line parameter.



回答3:

To my knowledge, FxCop 1.35 and onwards use two sources for the dictionary.

  • The Microsoft Office dictionary (adding words via MS Word etc. should work)
  • A 'CustomDictionary.xml' file stored in the FxCop program folder, rather than the project file folder.


回答4:

I am running FxCopCmd.exe as part of my build process, and here I pass the dictionary command line parameter

FxCopCmd.exe ... /dictionary:FxCopDictionary.xml

This work with the visual studio 2010 RC version of FxCop. Don't know if it'll work with previous versions.



回答5:

Further to David Schmitt's answer above, you may need to alter/remove the custom dictionary that ships with FxCop from within the FxCop installation folder.

I've just installed FxCop v1.36, and couldn't get the Project specific words in my dictionary to be recognised until I removed the custom dictionary at C:\Program Files\Microsoft FxCop 1.36\CustomDictionary.xml.

Once, I'd moved that to one side, FxCop started using the CustomDictionary.xml in the solution folder.

This does beg the question of how to add common words to a shared custom dictionary and project/solution specific words within other custom dictionaries and actually having FxCop use both. But for now it's working....



回答6:

I struggled with most of the above answers and kept searching for a solution. I finally made this change to my .fxcop file and checked it into source control, it works both from the command line and gui fxcop

I changed this line

<CustomDictionaries SearchFxCopDir="True" SearchUserProfile="True" SearchProjectDir="True" />

to

<CustomDictionaries SearchFxCopDir="True" SearchUserProfile="True" SearchProjectDir="True" >
  <CustomDictionary Path="$(ProjectDir)/CustomDictionary.xml"/>
</CustomDictionaries>


回答7:

Also make sure the file really is *.xml and not *.xml.txt. Then feel silly for creating a file with notepad and forgetting that it always adds a txt extension. That's what it turned out to be for me.



回答8:

I didn't have to set Build Action to be CodeAnalysisDictionary. In fact, I didn't even have that as an option (using Visual Studio 2010 Professional). I'm using the FxCop Integrator extension. It allows you to right-click on the Solution and choose Code Analysis. The other option is More Tasks. Within that More Tasks menu is the ability to edit Edit FxCop Dictionary. When I performed the edit, it created a new Solution Folder called Code Analysis containing a new XML file called FxCopDictionary.xml. This XML file is set up as one would expect the CustomDictionary.xml file to look like.

Hope this helps someone out there!