ConditionalAttribute クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した条件付きコンパイル シンボルが定義されていない場合、メソッド呼び出しまたは属性を無視するようコンパイラに指示します。
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の使用方法を示します。 この例では、/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中間言語 (MSIL) にコンパイルしないことをコンパイラに示します。 void を返さないメソッドにこの属性を適用すると、Visual Studioでコンパイル エラーが発生します。 属性に ConditionalAttribute を適用すると、条件付きコンパイル シンボルが定義されていない限り、属性をメタデータに出力しないことを示します。 メソッドまたは属性に渡されるすべての引数は、コンパイラによって引き続き型チェックされます。
次の手法を使用して、条件付きコンパイル シンボルを定義できます。
コンパイラ コマンド ライン オプションを使用する。たとえば、 /define:DEBUG です。
オペレーティング システム シェルで環境変数を使用する。たとえば、 DEBUG=1 を設定します。
ソース コードでプラグマを使用する。たとえば、コンパイル変数を次のように定義します。
#define DEBUG#Const DEBUG=True変数を未定義にするには、次のコマンドを使用します。
#undef DEBUG#Const DEBUG=False
共通言語仕様 (CLS) に準拠しているコンパイラは、 ConditionalAttributeを無視できます。 C#、F#、Visual Basic、および C++ コンパイラでは、ConditionalAttributeがサポートされています。JScript コンパイラでは、この属性はサポートされていません。
Note
Visual Basicでは、AddressOf 演算子はこの属性の影響を受けません。 たとえば、 Call CType(AddressOf delegate, Action) は常に delegateを呼び出しますが、 Call delegate() は呼び出さない場合があります。
ConditionalAttribute は、 Debug および Trace クラスで定義されているメソッドに適用されます。
属性の使用方法の詳細については、「 属性」を参照してください。
コンストラクター
| 名前 | 説明 |
|---|---|
| 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) |