SuppressMessageAttribute 類別

定義

隱藏特定程式碼分析規則違規的報告,允許在單一程式碼成品上進行多個隱藏。 不適用於編譯器診斷。

public ref class SuppressMessageAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=true, Inherited=false)]
[System.Diagnostics.Conditional("CODE_ANALYSIS")]
public sealed class SuppressMessageAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=true, Inherited=false)>]
[<System.Diagnostics.Conditional("CODE_ANALYSIS")>]
type SuppressMessageAttribute = class
    inherit Attribute
Public NotInheritable Class SuppressMessageAttribute
Inherits Attribute
繼承
SuppressMessageAttribute
屬性

範例

下列程式碼範例示範如何使用 SuppressMessageAttribute 屬性來隱藏效能警告訊息。

#define CODE_ANALYSIS
using System;
using System.Diagnostics.CodeAnalysis;

namespace CodeAnalysisSample
{
    class Library
    {
        [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "isChecked")]
        [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "fileIdentifier")]
        static void FileNode(string name, bool isChecked)
        {
            string fileIdentifier = name;
            string fileName = name;
            string version = String.Empty;
        }
    }
}
#Const CODE_ANALYSIS = True
Imports System.Diagnostics.CodeAnalysis



Class Library
    
    <SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId:="isChecked"), _
     SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId:="fileIdentifier")> _
    Shared Sub FileNode(ByVal name As String, ByVal isChecked As Boolean)
        Dim fileIdentifier As String = name
        Dim fileName As String = name
        Dim version As String = String.Empty

    End Sub
End Class

備註

這個屬性可以套用至任何應用程式專案。 如需可隱藏之違規的完整清單,請參閱 程式碼品質規則

注意

ConditionalAttribute會套用至這個類別,將前置處理符號 「CODE_ANALYSIS」 指定為條件式符號,以決定是否包含或省略屬性呼叫。 如果已定義符號,則會包含屬性呼叫;否則會省略呼叫。

當您在 Visual Studio 中以滑鼠右鍵按一下程式碼分析警告,然後選擇 [隱藏檔案] 中的 [隱藏 < 規則 >>識別碼] 或 [隱藏 < 來源 (屬性中的規則 >> 識別碼) 時,就會將SuppressMessage屬性新增至全域歸併檔案或原始程式碼檔案。 如需 SuppressMessage 屬性及其在 Visual Studio 中使用的詳細資訊,請參閱 隱藏程式碼分析違規

建構函式

SuppressMessageAttribute(String, String)

初始化 類別的新實例 SuppressMessageAttribute ,並指定程式碼分析工具的類別和分析規則的識別碼。

屬性

Category

取得分類,識別屬性的分類。

CheckId

取得要隱藏之程式碼分析工具規則的識別碼。

Justification

取得或設定隱藏程式碼分析訊息的對齊。

MessageId

取得或設定展開的排除準則。

Scope

取得或設定與屬性相關的程式碼範圍。

Target

取得或設定代表程式碼分析目標的完整路徑。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於