Code Analysis on a Code Generator Generated File - How to Suppress Warnings?
We run Code Analysis on all our source files. One of our source files is a Linq-To-SQL generated file, which we have no control over the generated output. The generated code is producing Code Analysis warnings that I would like to suppress. Is there a开发者_高级运维ny way I can suppress CA warnings in a code generated file that doesn't involve creating attributes and/or pragma's in the code itself (which will get overwritten each time the file is generated)?
Do your classes have the [GeneratedCode] attribute? If so you can get FxCop to ignore them:
Using an FxCop project:
- Open your FxCop project in FxCop
- Choose Project -> Options -> Spelling & Analysis
- Check Suppress analysis results against generated code
- Click OK
Via the command-line:
- Pass the /ignoregeneratedcode switch, for example:
FxCopCmd.exe /file:MyAssembly.dll /out:AnalysisResults.xml /ignoregeneratedcode
http://blogs.msdn.com/fxcop/archive/2008/02/28/faq-how-do-i-prevent-fxcop-1-36-from-firing-warnings-against-generated-code.aspx
You can work around the lack of GeneratedCode
attribute by using your own branch of the partial classes to apply that attribute. This will mean that any custom code you add (including implementing partial methods) will be excluded. Eg.:
namespace MyApp.DB {
[GeneratedCode("LINQ To SQL", "4.0")]
internal partial class MyAppDataContext {
}
// Repeat for each entity
}
If you are using the FxCop GUI you could simply exclude these issues within the FxCop project. Just right click the issue and select Exclude where you can than also add a comment.
But if you run FxCop in the Output Window I do not have a clue. Maybe you could check if it is possible to create a module-level SuppressMessage and paste it into the AssemblyInfo.cs. But I don't think so.
The PLINQO (Linq-to-SQL) CodeSmith templates also generate this attribute for you automatically. Most add-in's are also starting to ignore partial classes that are generated with ".generated" in the filename.
[System.CodeDom.Compiler.GeneratedCode("CodeSmith", "5.0.0.0")]
精彩评论