加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

What is the GlobalSuppressions.cs/GlobalSuppressions.vb file

发布时间:2020-12-16 23:17:31 所属栏目:大数据 来源:网络整理
导读: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 GlobalSuppr

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:

  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 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:

  1. The user right-clicks on a warning raised against an assemblyand 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 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:

  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):

    <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?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读