23

Is it possible to exclude a complete namespace from all FxCop analysis while still analyzing the rest of the assembly using the SuppressMessageAttribute?

In my current case, I have a bunch of classes generated by LINQ to SQL which cause a lot of FxCop issues, and obviously, I will not modify all of those to match FxCop standards, as a lot of those modifications would be gone if I re-generated the classes.

I know that FxCop has a project option to suppress analysis on generated code, but it does not seem to recognize the entity and context classes created by LINQ 2 SQL as generated code.

1
  • This is an exact duplicate because the other question is also asking about Linq-to-SQL; but I think the answers should be merged, and the stub maintained for search-ability. Sep 1, 2010 at 14:37

4 Answers 4

18

If you tag your classes with the [GeneratedCode] attribute, you can use the /ignoregeneratedcode flag with FxCop as described in this MSDN post:

FAQ: How do I prevent FxCop from firing warnings against generated code

You may have to add a new code file and implement new partial classes there to add the attribute to the classes:

[GeneratedCode]
public partial class MainDataContext { }

Just make sure you add everything to the correct namespace when you create your new file.

2
  • How this adress the question? the question was about a namespace, this only fix it for a single class Jun 18, 2011 at 2:27
  • 1
    @Juan - Read the question more. The OP is really asking how to exclude "a bunch of classes generated by LINQ to SQL". This is the solution for that problem. Unfortunately, no solution exists for an entire namespace. Jun 18, 2011 at 2:30
5

Add a [GeneratedCode] attribute to the classes.
EDIT: I meant to partial classes with the same names, as explained by the other answer.

0
1

Use the Generated Code Attribute, heres the blog post from the Code Analysis team on the subject.

This at the top of the namespace should do the trick:

[GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
2
  • Unfortunately, one cannot add those attributes to namespaces, as far as I know. That would mean I would have to add it to each generated class manually - that cannot be the ideal solution. :D
    – hangy
    Dec 9, 2008 at 16:02
  • 4
    pretty much. The real pain is if you have to regenerate the code for any reason you have to drop the attribute on again. If the classes Linq to Sql generates are partials, you can maintain the attributes away from the generated code. I have a similar issue with EF classse.
    – Chane
    Dec 9, 2008 at 17:04
1
<Assembly: SuppressMessage("Microsoft.Design", _
    "CA1020:AvoidNamespacesWithFewTypes", _
    Scope:="namespace", _
    Target:="Missico.IO")> 

Put statement in GlobalSuppressions.vb at root of project.

All I have is VB example.

1
  • This doesn't work for many of the code generation warnings in VS 2010 Code Analysis: don't seem to be suppressible at namespace level. But there is another option: stackoverflow.com/questions/2221881/…
    – Richard
    Apr 29, 2010 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.