来自:http://blog.csdn.net/yefengmeander/article/details/6572647
As usual there are times when we love cops (when they rescue us from problems). However there are times when we hate say ohh god not cop again (when they catch us or fine).
FxCop , an abbreviation "Framework Police," is a rules-based engine that checks managed code assemblies for Microsoft's .NET Framework design guidelines and custom guidelines. FxCop is a rule based engine which helps the development team to follow best practices of the .NET platform and development standards (based on every company standards).
You can apply to any assembly which been created using any language that target Microsoft.NET managed environment.
Once you install FxCop , Go to Start Menu -> All Programs -> "Microsoft FxCop " option and then click on FxCop . It will launch an empty window similar to the one shown below. It contains 3 panes – configuration pane, message pane and properties pane.
Go to Project Menu and click on “Add Targets…”. Select the EXE or DLL. Once it is done, click on Analyze button and your screen will look like below:
To analyze and which rules to check load, assemblies/exe. There are 2 tabs at the top of pane (Targets and Rules). It can load more than one assembly into the project by clicking on "Add Target for Analysis" button in toolbar or through Menu "Project->Add Targets”. It shows you all the resources included in the assembly, all the namespaces in the assembly and for each namespace, all the types. Drilling deeper with the plus sign in front of each type, you can show all the members. It shows the constructors, methods, properties and fields.
Selecting a resource, assembly, type or member of a type shows the following information in the property pane:
Right clicking on a type member, you can select from the "View | IR" popup menu to show the IL code for this member.
There are by default nine groups of rules:
Right click on the rules list displays a popup menu which helps to group the rules in three categories:
Click on the Analyze button in the toolbar while doing so shows a progress bar, and then fills the message pane with all the messages it found. On top of the pane, you see three buttons – Active, Excluded and Absent.
The Active list shows you all the active messages. These are all the errors and warnings FxCop has found with the last analysis performed. By Default, display four columns in that list:
You can right click in the list and select "configure columns" from the popup menu. This allows you to show some additional columns like when was the issue seen first time (Created column), when was the issue seen last time (Last Seen column), is this a new issue (New column), etc.
Selecting a message shows you a summary in the properties pane. It includes the target item which caused the message, a short resolution description and also a help link which shows you a detailed description of the issue and the resolution for it. You can also double click on a message which brings up a dialog box with the same information but differently organized:
The "Message Details" dialog box has five tabs.
You can also filter the messages by clicking on a group of rules, an individual rule or an assembly, namespace, type or type member. Selecting the top node in the Targets or Rule tab shows again all messages. For example, selecting a type allows you to work through all messages of that type. Another example is if you are working on a certain type member, selecting it allows you to view and work through all the messages of this type member. Or maybe you just resolved a certain issue and you want to see any other occurrence of it. So you click on the specific rule which created the message and it allows you to view and work through any other occurrence of that message.
You need to work through all messages one-by-one. A message can describe an issue in your code which needs to be resolved or you determine after careful review that it is a false-positive. If it is a false-positive meaning FxCop believes it to be an issue but you determined after careful review that this is not an issue then you mark it as excluded. You right click on the message and select Exclude from the popup menu. This can also be done by double clicking on the message and clicking on the Exclude button in the "Message Details" dialog. In the upcoming "Edit Notes" dialog box, you enter a summary why it is a false-positive and then click Ok. This moves the message from the Active list to the Excluded list. FxCop will exclude this message from now on every time you perform another analysis. Clicking on the Excluded button on top of the message pane will show you all the excluded messages. Double clicking on the message shows you again the "Message Details" dialog and the Notes tab allows you to view the notes you entered when marking the message as excluded. You can move a message back to the active list by right clicking on it and selecting "Mark as Active" from the popup menu. You can also do this by double clicking on the message and selecting the Unexclude button on the "Message Details" dialog.
You have two ways to remove a message from the Active list after you have resolved the issue. You can right click on the message and select "Mark as Absent" from the popup menu or you just run another analysis and FxCop detects that the issue has been resolved and moves it to the Absent list for you. Don't forget to recompile your code as FxCop doesn't look at the source code but rather the compiled assembly. I consider it good practice to add some notes to the message after it has been moved to the Absent list. This provides a historical view and allows to go back and understand why you have made certain changes. Clicking on the Absent button on top of the message pane shows you all the resolved issues.
Saving your FxCop project only saves active and excluded messages but not the Absent ones. So if you want to keep a historical view go to menu "Project | Options" and on the "Project Options" dialog, on the Standard tab, select the Absent checkbox in the "Save Messages" group. This changes the option only for the current project. Going to "Tools | Settings" menu shows the Settings dialog. On the "Project defaults" tab, select the Absent checkbox under the "Save Messages" group. This setting takes then effect for all future FxCop projects you create. I recommend keeping the historical view because I always find it useful to be able to go back and understand what has been changed on a project over time, especially if you suddenly run into un-explainable exceptions, crashes or other difficulties with your product.
This pane has two tabs. The properties tab shows information about the selected assembly, namespace, type, type member, group of rules, rule or message. We have covered that already in detail. The Output tab shows informational, warning and error messages generated by the rules. These messages only appear if the TraceGeneral trace-switch in the FxCop .exe .config file is enabled.
Click on the menu "File | Save Report As" and provide a name and location for the report. The report is stored as an XML document which references a XSLT document - FxCop Report.xsl . Opening the report with your browser uses the referenced XSLT document to render a HTML report. The report allows you to drill down to all messages on the assembly level, type level and type member level.
The report, by default, only includes active messages. So, if you want to keep a historical view, go to menu "Project | Options" and on the "Project Options" dialog, on the Standard tab, select the Excluded and Absent checkbox in the "Save Messages" group (for reporting). This changes the option only for the current project. Going to "Tools | Settings" menu shows the Settings dialog. On the "Project defaults" tab, select the Excluded and Absent checkbox under the "Save Messages" group (for reporting). This setting then takes effect for all future FxCop projects you create. Try to include the historical view in the generated reports because it is useful to be able to go back and understand what has been changed on a project over time.
To set up FxCop as an external tool in Visual Studio:
Note : If you had created custom rules and want to apply to all projects, place the custom rule DLL to c:/Program Files/Microsoft FxCop 1.36/Rules .
The External Tools configuration dialog box is displayed. The following screen shot shows the dialog box after the steps in this procedure have been completed.
Done. Now you just need to run FxCop .
Go to Tools –> FxCop analyze your code and because we checked “Use Output window” in External Tools dialog, result should appear in Visual Studio output window.