views:

1616

answers:

5

Hi,

In a project I'm working on FxCop shows me lots of (and I mean more than 400) errors on the InitializeComponent() methods generated by the Windows Forms designer. Most of those errors are just the assignment of the Text property of labels.

I'd like to suppress those methods in source, so I copied the suppression code generated by FxCop into AssemblyInfo.cs, but it doesn't work.

This is the attribute that FxCop copied to the clipboard.

[module: SuppressMessage("Microsoft.Globalization",
    "CA1303:DoNotPassLiteralsAsLocalizedParameters",
    Scope = "member",
    Target = "WindowsClient.MainForm.InitializeComponent():System.Void",
    MessageId = "System.Windows.Forms.Control.set_Text(System.String)")]

Anyone knows the correct attribute to suppress this messages?

Thanks.

PS: I'm using Visual Studio 2005, C#, FxCop 1.36 beta.

+10  A: 

You've probably got the right code, but you also need to add CODE_ANALYSIS as a precompiler defined symbol in the project properties. I think those SuppressMessage attributes are only left in the compiled binaries if CODE_ANALYSIS is defined.

Matt Hamilton
+1  A: 

Module level suppression messages need to be pasted into the same file as the code that is raising the FxCop error before the namespace declaration or in assemblyinfo.cs. Additionally, you will need to have CODE_ANALYSIS defined as a conditional compiler symbols (Project > Properties > Build). Once that is in place, do a complete rebuild of project and the next time you run FxCop the error should be moved to the "Excluded in Source" tab.

Also, one small tip, but if you are dealing with a lot of FxCop exclusions it might be useful to wrap a region around them so you can get them out of the way.

Rob
+2  A: 

@Rob

Module level suppression messages need to be pasted into the same file as the code that is raising the FxCop error, before the namespace declaration.

That's not true. Module-level suppression attributes can live in assemblyinfo.cs. That's where I normally put them.

Matt Hamilton
I actually hadn't heard that yet, but now that I have that is definitely the way that I am going to start setting up some of my projects. Thanks for the tip.
Rob
I just tried adding module-level excludes to my assemblyinfo.cs file, but the errors still get reported. I left all attributes, and also tried removing scope, messageid, and target, and it always gets reported.
Dave
+2  A: 

In FxCop 1.36 there is actually a project option on the "Spelling & Analysis" tab that will supress analysis for any generated code.

If you don't want to turn analysis off for all generated code, you need to make sure that you add a CODE_ANALYSIS symbol to the list of conditional compilation symbols (project properties, Build tab). Without this symbol defined, the SupressMessage attributes will be removed from the compiled code so FxCop won't see them.

The other problem with your SuppressMessage attribute is that you are listing a "Target" of a specific method name (in this case WindowsClient.MainForm.InitializeComponent():System.Void) and listing a specific "Scope". You may want to try removing these; otherwise you should add this SuppressMessage to each instance of the method.

You should also upgrade to the RTM version of FxCop 1.36, the beta will not automatically detect the newer version.

Scott Dorman
A: 

Thanks Matt, that was the problem.

Julio César