Suppressing "CA2210:AvoidNamespacesWithFewTypes" in GlobalSuppressions.cs

Locked Suppressing "CA2210:AvoidNamespacesWithFewTypes" in GlobalSuppressions.cs

  • Wednesday, December 09, 2009 7:13 AM
     
     
    Hello,

    I'm having the problem in suppressing  "CA2210:AvoidNamespacesWithFewTypes" in GlobalSuppressions.cs. (Note: Suppressing "CA2210:AssembliesShouldHaveValidStrongNames" is working but not  "CA2210:AvoidNamespacesWithFewTypes") 

    I tried the code below in GlobalSuppressions.cs

    [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA2210:AvoidNamespacesWithFewTypes")]

    But not working.

    Any idea why?

    Another question is that why I need to suppress XamlGeneratedNamespace?

    [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "XamlGeneratedNamespace")]

    Michael Sync: blog: http://michaelsync.net

All Replies

  • Wednesday, December 09, 2009 9:08 AM
    Moderator
     
     
    Hello Michael,

    The check ID of rule "AvoidNamespacesWithFewTypes" is CA1020 in Visual Studio 2008. Please use
    [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes")] in stead.

    The voilation of rule CA1020: AvoidNamespaceWithFewTypes because "A namespace other than the global namespace contains fewer than five types." (from MSDN: Avoid namespace with few types), however, we could omit that rule or suppress it in source code / global suppression file.

    Best regards
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
  • Thursday, December 10, 2009 2:12 AM
     
     
    Hello Roahn,

    I put this code in GlobalSuppressions.cs but still not working. Is it working in your machine?

    [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes")]

    Michael Sync: blog: http://michaelsync.net
  • Thursday, December 10, 2009 4:13 AM
    Moderator
     
     
    Hello Michael,

    Please add the Target and try again, for example:
    [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "XamlGeneratedNamespace")]

    Let me know if this works,
    Best regards
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
  • Thursday, December 10, 2009 5:19 AM
     
     
    It didn't work the way that I like. It works only for suppressing XamlGeneratedNamespace. What I'm looking for is to suppress this rule for all of my classses. 

    As of now, I have the following in my global suppression file. 

    [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA2210:AssembliesShouldHaveValidStrongNames")]


    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "MichaelSync.Project")]
    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "MichaelSync.Project.Extensions")]
    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "MichaelSync.Project.Reports")]
    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "MichaelSync.Project.AttachedProperties")]
    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "MichaelSync.Project.Enumeration")]
    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "'MichaelSync.Project.Converters")]
    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "MichaelSync.Project.Services")]
    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "MichaelSync.Project.Comparers")]
    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "MichaelSync.Project.Converters")]
    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "XamlGeneratedNamespace")]
    [assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "MichaelSync.Project.Enumeration")]


    I don't want to write this line for each namespace. 

    So, I like to write just one line as below. 

    [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes")]

    Is it possible to do that? 

    Michael Sync: blog: http://michaelsync.net
  • Thursday, December 10, 2009 6:10 AM
    Moderator
     
     
    Hello Michael,

    Thanks for your feedback!
    Unfortunately, FxCop did not support using just one suppression to suppress multiple violations. But we have other workarounds:
     If we use FxCop.exe, we could uncheck those classes/namespaces;
     While, if we use Visual Stduio, we could disable Rule CA1020. (please go to Property page -> Code Analysis tab and disable it)

    Best regards
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
  • Thursday, December 10, 2009 9:24 AM
     
     
    Hello,

    Can we disable the rule from GlobalSuppressions.cs? 

    The reason is that if I disable the rule and check-in the file to TFS then the whole team who are using FxCop will get it. Otherwise, each and everyone will have to do it for the same thing.

    Michael Sync: blog: http://michaelsync.net
  • Thursday, December 10, 2009 2:00 PM
     
     
    Can we disable the rule from GlobalSuppressions.cs?
    No.
    The reason is that if I disable the rule and check-in the file to TFS then the whole team who are using FxCop will get it.
    Isn't this your intent?  From your prior posts in this thread, it sounds like you want to disable the rule for at least an entire project.  If you want to do this for just one project out of several, there are approaches for this in both FxCop and VS Static Analysis, but they differ, so could you please let us know which one you are using?
  • Tuesday, December 15, 2009 5:02 AM
     
     
    It would be the best if I can use GlobalSuppressions.cs for entire solution. 

    But I'm still okay if I can disable the rule from GlobalSuppressions.cs to each project. 

    >> If you want to do this for just one project out of several, there are approaches for this in both FxCop

    I'm currently using FxCop because we are using VS 2008 Professional Edition. 

    Thanks.

    Michael Sync: blog: http://michaelsync.net
  • Tuesday, December 15, 2009 1:23 PM
     
     Answered
    It would be the best if I can use GlobalSuppressions.cs for entire solution.
    You can share exclusions between projects, but you'll still need to create an exclusion for each namespace (by name only, not by name and assembly).  Here's the basic approach: 
    1. Create a file named SharedSuppressions.cs (or whatever name you prefer) at the solution level.
    2. Place the CA1020 suppressions that you want to share between projects in the new file.
    3. Link the file into each of your projects using the approach described at http://blogs.msdn.com/shawnfa/archive/2006/04/24/582278.aspx.
    But I'm still okay if I can disable the rule from GlobalSuppressions.cs to each project. 

    If you want to disable the rule entirely, why don't you simply exclude it in your FxCop project (by untoggling the checkbox next to the rule node in the rules tree)?  If you want to exclude it only for some VS projects when using FxCop, you should create two FxCop projects: one for the VS projects in which the rule should be enabled, and one for the VS projects in which it should be disabled.