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 コンパイラ オプションで定義されていることを前提としています。 コンパイラ オプションを変更することで、さまざまな結果を取得できます。 必要に応じて、コンパイラ オプションとして識別するのではなく、サンプル コードでプラグマを使用して条件を定義できます。

#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 intermediate language (MSIL) にコンパイルしないことを、コンパイラに指示することを示します。 void ではないものを返すメソッドにこの属性を定義する場合、Visual Studio でコンパイル エラーが表示されます。 ConditionalAttribute を属性に適用することは、条件付きコンパイル シンボルが定義されていない場合は、メタデータに属性を出力しないことを示します。 メソッドあるいは属性に渡されるどんな引数でも、コンパイラによって型チェックが行われます。

次の手法を使用して、条件付きコンパイル シンボルを定義できます。

  • /define:DEBUG のように、コンパイラのコマンド ライン オプションを使用します。

  • set DEBUG=1 のように、オペレーティング システムのシェルで環境変数を使用します。

  • ソース コードでのプラグマを使用します。たとえば、次の例のようにコンパイル変数を定義します。

    #define DEBUG  
    
    #Const DEBUG=True  
    

    変数を未定義にするには、次のコマンドを使用します。

    #undef DEBUG  
    
    #Const DEBUG=False  
    

共通言語仕様 (CLS) に準拠するコンパイラは、ConditionalAttribute を無視することを許可されています。 C#、F#、Visual Basic、および C++ コンパイラはサポートConditionalAttributeしています。JScript コンパイラは属性をサポートしていません。

注意

Visual Basic で、AddressOf演算子はこの属性の影響を受けません。 たとえば、Call CType(AddressOf delegate, Action) は常に delegate を呼び出しますが、Call delegate() はそうしません。

ConditionalAttribute は、DebugTrace クラスで定義されているメソッドに適用されます。

属性を使用する方法の詳細については、属性を参照してください。

コンストラクター

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)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象