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 awarning and choose Suppress Message(s),Code Analysis checks to see if the warning was raised against an element thatexists within source. If it does,suchas in the case of a type or a member,then the suppression is applied against the element itself. This is called In-Source Suppression because the suppression is applied in-sourcealongside the targetof the warning.
The following sequence shows this:
- The user right-clicks on a warning raised against a property and chooses Suppress Message(s).
- 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 terminologyand 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:
- The user right-clicks on a warning raised against an assemblyand chooses Suppress Message(s).
- 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 anin-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:
-
Open your project in Visual Studio
- In Solution Explorer,right-click the project and choose Properties
- In the Project Properties window,choose the Code Analysis tab
- In the Code Analysis tab,choose the rules you want for your minbar
- In Solution Explorer,right-click the project and choose Unload Project,answering Yes to any prompt to save changes
- In Solution Explorer,right-click the project and choose Edit
- 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):
<ProjectDefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<CodeAnalysisModuleSuppressionsFile>[name]</CodeAnalysisModuleSuppressionsFile>[...]
</Project>
If you want all your projects to use the same name,see thefollowing: FAQ: How do I share Managed Code Analysis rule settings over multiple projects?
原文链接:https://www.f2er.com/vb/262402.html