ConditionalAttribute 類別

定義

表示編譯器應該忽略方法呼叫或屬性,除非已定義指定的條件式編譯符號。

public ref class ConditionalAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)]
public sealed class ConditionalAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=true)]
[System.Serializable]
public sealed class ConditionalAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ConditionalAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)>]
type ConditionalAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=true)>]
[<System.Serializable>]
type ConditionalAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ConditionalAttribute = class
    inherit Attribute
Public NotInheritable Class ConditionalAttribute
Inherits Attribute
繼承
ConditionalAttribute
屬性

範例

下列範例示範 ConditionalAttribute 的用法。 此範例假設條件是使用 /define 編譯程序選項來定義。 您可以變更編譯程式選項來取得不同的結果。 您可以選擇性地在範例程式代碼中使用 pragmas 來定義條件,而不是將它們識別為編譯程序選項。

#define CONDITION1
#define CONDITION2
using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {
        Console.WriteLine("Calling Method1");
        Method1(3);
        Console.WriteLine("Calling Method2");
        Method2();

        Console.WriteLine("Using the Debug class");
        Debug.Listeners.Add(new ConsoleTraceListener());
        Debug.WriteLine("DEBUG is defined");
    }

    [Conditional("CONDITION1")]
    public static void Method1(int x)
    {
        Console.WriteLine("CONDITION1 is defined");
    }

    [Conditional("CONDITION1"), Conditional("CONDITION2")]
    public static void Method2()
    {
        Console.WriteLine("CONDITION1 or CONDITION2 is defined");
    }
}

/*
When compiled as shown, the application (named ConsoleApp)
produces the following output.

Calling Method1
CONDITION1 is defined
Calling Method2
CONDITION1 or CONDITION2 is defined
Using the Debug class
DEBUG is defined
*/
#Const CONDITION1 = True
#Const CONDITION2 = True
Imports System.Diagnostics

Class Test

    Shared Sub Main()
        Console.WriteLine("Calling Method1")
        Method1(3)
        Console.WriteLine("Calling Method2")
        Method2()
        
        Console.WriteLine("Using the Debug class")
        Debug.Listeners.Add(New ConsoleTraceListener())
        Debug.WriteLine("DEBUG is defined")
    End Sub
       
    <ConditionalAttribute("CONDITION1")> _
    Shared Sub Method1(x As Integer)
        Console.WriteLine("CONDITION1 is defined")
    End Sub
    
    <ConditionalAttribute("CONDITION1"), ConditionalAttribute("CONDITION2")> _
    Shared Sub Method2()
        Console.WriteLine("CONDITION1 or CONDITIOIN2 is defined")
    End Sub
    
End Class


' When compiled as shown, the application (named ConsoleApp) 
' produces the following output.

'Calling Method1
'CONDITION1 is defined
'Calling Method2
'CONDITION1 or CONDITION2 is defined
'Using the Debug class
'DEBUG is defined

備註

您可以將 屬性套用 ConditionalAttribute 至方法和類別。 不過,其對類別的使用僅適用於衍生自 Attribute的類型。 ConditionalAttribute 如果您將它套用至任何其他類型,則會忽略或產生編譯程式警告或錯誤訊息。

ConditionalAttribute 用至方法表示除非已定義與 ConditionalAttribute 相關聯的條件式編譯符號,否則對 方法的呼叫不應編譯成 Microsoft 中繼語言 (MSIL) 。 如果您將此屬性套用至未傳回 void 的方法,您將會在 Visual Studio 中收到編譯錯誤。 套 ConditionalAttribute 用至屬性表示除非定義條件式編譯符號,否則不應將屬性發出至元數據。 任何傳遞給方法或屬性的自變數,仍會由編譯程式檢查類型。

您可以使用下列技術來定義條件式編譯符號:

  • 使用編譯程式命令行選項;例如 ,/define:DEBUG

  • 在操作系統殼層中使用環境變數;例如, 設定 DEBUG=1

  • 在原始程式碼中使用 pragmas;例如,定義編譯變數,如下所示:

    #define DEBUG  
    
    #Const DEBUG=True  
    

    若要取消定義變數,請使用下列命令:

    #undef DEBUG  
    
    #Const DEBUG=False  
    

允許符合 Common Language Specification (CLS) 的編譯程式忽略 ConditionalAttribute。 C#、F#、Visual Basic 和 C++ 編譯程序支援 ConditionalAttribute;JScript 編譯程式不支援 屬性。

注意

在 Visual Basic 中 AddressOf ,運算子不會受到這個屬性的影響。 例如,Call CType(AddressOf delegate, Action)一律會叫用 ,但Call delegate()可能不會叫用 delegate

ConditionalAttribute會套用至和 Trace 類別中Debug定義的方法。

如需如何使用屬性的詳細資訊,請參閱 屬性

建構函式

ConditionalAttribute(String)

初始化 ConditionalAttribute 類別的新執行個體。

屬性

ConditionString

取得與 ConditionalAttribute 屬性相關聯的條件式編譯符號。

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)

適用於