What is the GlobalSuppressions.cs/GlobalSuppressions.vb file and why is it needed

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 Suppression because 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="http://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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值