Partager via


FAQ: What is the GlobalSuppressions.cs/GlobalSuppressions.vb file and why is it needed? Is it possible to change the name of this file? [David Kean]

I've noticed that Code Analysis sometimes places suppressions in a file called GlobalSuppression.cs (GlobalSuppressions.vb in Visual Basic). Why this is file needed and it is possible to change its name?

What is this file?

When you right-click on a warning and choose Suppress Message(s) , Code Analysis checks to see if the warning was raised against an element that exists within source. If it does, such as in the case of a type or a member, then the suppression is applied against the element itself. This is called In-Source Suppressionbecause the suppression is applied in-source alongside the target of the warning.

The following sequence shows this:

  1. The user right-clicks on a warning raised against a property and chooses Suppress Message(s) .

  2. The suppression (via use of the SuppressMessageAttribute) is applied against the property.

If the warning was raised against an element that does not live in source, such as namespaces * , assemblies and or any other element without source information (ie compiler generated constructors), then Code Analysis places the suppression, by default, in a file called GlobalSuppression.cs (GlobalSuppression.vb in Visual Basic). This is currently called Module-Level Suppression or Assembly-Level Suppression because the suppression is applied at the assembly-level using the [assembly:] declarator, however, as warnings can be raised and suppressed against both a module and assembly at this level, this is confusing terminology and in future versions of Visual Studio this name will likely change.

* Although technically namespaces do live within source files, they do not have a representation in the Common Intermediate Language (CIL) and therefore you cannot apply attributes against them.

The following sequence shows this:

  1. The user right-clicks on a warning raised against an assembly and chooses Suppress Message(s) .

  2. The suppression (via use of the SuppressMessageAttribute) is applied against the assembly in GlobalSuppression.cs.

As you can see from above, GlobalSuppression.cs/GlobalSuppression.vb exists to store suppressions against elements without source information. If this file does not exist within the project, then Code Analysis will automatically create it.

Note: Starting in Orcas, it will become possible to choose whether to apply a suppression against a element containing source information as an in-source or module-level suppression.

How do I change the name of this file?

While not very obvious, it is possible to change the name of the Global Suppression file that Code Analysis stores these Module-Level Suppressions in.

To change the name, do the following:

  1. Open your project in Visual Studio

  2. In Solution Explorer, right-click the project and choose Properties

  3. In the Project Properties window, choose the Code Analysis tab

  4. In the Code Analysis tab, choose the rules you want for your minbar

  5. In Solution Explorer, right-click the project and choose Unload Project, answering Yes to any prompt to save changes

  6. In Solution Explorer, right-click the project and choose Edit

  7. Add the following <CodeAnalysisModuleSuppressionsFile> element to the project, replacing [name] with the new name (without path information) of the Global Suppressions file (for example Suppressions.cs):

     <Project DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003"> 

       <PropertyGroup> < CodeAnalysisModuleSuppressionsFile> [name] </CodeAnalysisModuleSuppressionsFile>

       [...]

     </Project>

     

If you want all your projects to use the same name, see the following: FAQ: How do I share Managed Code Analysis rule settings over multiple projects?

Comments

  • Anonymous
    February 29, 2008
    We've been using the static code analysis in Visual Studio in a couple of projects by now, but I have