ConditionalAttribute 類別

定義

表示編譯器應該忽略方法呼叫或屬性,除非已定義指定的條件式編譯符號。Indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.

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

範例

下列範例示範 ConditionalAttribute 的用法。The following example demonstrates the use of ConditionalAttribute. 此範例假設條件是以 /define編譯器選項來定義。The example assumes that the condition is defined with the /define compiler option. 您可以藉由變更編譯器選項來取得不同的結果。You can obtain different results by changing the compiler option. 您可以選擇性地在範例程式碼中使用 pragma 來定義條件, 而不是將它們識別為編譯器選項。You can optionally define the conditions by using pragmas in the sample code instead of identifying them as compiler options.

#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) 套用至方法和類別。You can apply the ConditionalAttribute attribute to methods and classes. 不過, 其在類別上的用法僅適用于衍生自Attribute的類型。However, its use on classes is valid only for types that are derived from Attribute. ConditionalAttribute會略過, 或將會產生編譯器警告或錯誤訊息 (如果您將它套用至任何其他類型的話)。ConditionalAttribute either will be ignored or will produce a compiler warning or error message if you apply it to any other type.

將套用ConditionalAttribute至方法, 會指示編譯器呼叫方法不應編譯成 Microsoft 中繼語言 (MSIL), 除非已定義與相關聯的條件式編譯符號。 ConditionalAttributeApplying ConditionalAttribute to a method indicates to compilers that a call to the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with ConditionalAttribute is defined. 如果您將此屬性套用至不會傳回 void 的方法, 您將會在 Visual Studio 中收到編譯錯誤。You will get a compilation error in Visual Studio if you apply this attribute to a method that does not return void. 套用ConditionalAttribute至屬性工作表示除非定義條件式編譯符號, 否則不應將屬性發出至中繼資料。Applying ConditionalAttribute to an attribute indicates that the attribute should not be emitted to metadata unless the conditional compilation symbol is defined. 傳遞至方法或屬性的任何引數, 仍然由編譯器進行類型檢查。Any arguments passed to the method or attribute are still type-checked by the compiler.

您可以使用下列技巧來定義條件式編譯符號:You can use the following techniques to define conditional compilation symbols:

  • 使用編譯器命令列選項;例如, /define: DEBUGUse compiler command-line options; for example, /define:DEBUG.

  • 使用作業系統 shell 中的環境變數;例如,設定 DEBUG = 1Use environment variables in the operating system shell; for example, set DEBUG=1.

  • 在原始程式碼中使用 pragma;例如, 定義編譯變數, 如下所示:Use pragmas in the source code; for example, define the compilation variable as follows:

    #define DEBUG  
    
    #Const DEBUG=True  
    

    若要取消定義變數, 請使用下列各項:To undefine the variable, use the following:

    #undef DEBUG  
    
    #Const DEBUG=False  
    

符合 Common Language Specification (CLS) 的編譯器可以忽略ConditionalAttributeCompilers that comply with the Common Language Specification (CLS) are permitted to ignore ConditionalAttribute. C#、Visual Basic 和C++編譯器支援ConditionalAttribute; JScript 編譯器不支援屬性。The C#, Visual Basic, and C++ compilers support ConditionalAttribute; the JScript compiler does not support the attribute.

注意

在 Visual Basic 中, AddressOf運算子不會受到這個屬性的影響。In Visual Basic, the AddressOf operator is not affected by this attribute. 例如, Call CType(AddressOf delegate, Action) Call delegate()一律會叫用,但可能不會叫用。delegateFor example, Call CType(AddressOf delegate, Action) always invokes delegate, although Call delegate() might not.

ConditionalAttribute會套用至定義于DebugTrace類別中的方法。ConditionalAttribute is applied to the methods that are defined in the Debug and Trace classes.

如需如何使用屬性的詳細資訊, 請參閱屬性For more information about how to use attributes, see Attributes.

建構函式

ConditionalAttribute(String)

初始化 ConditionalAttribute 類別的新執行個體。Initializes a new instance of the ConditionalAttribute class.

屬性

ConditionString

取得與 ConditionalAttribute 屬性相關聯的條件式編譯符號。Gets the conditional compilation symbol that is associated with the ConditionalAttribute attribute.

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

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

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

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

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

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

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於