ConditionalAttribute クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ <AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Method, AllowMultiple:=True)> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class ConditionalAttribute Inherits Attribute
[SerializableAttribute] [AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple=true)] [ComVisibleAttribute(true)] public sealed class ConditionalAttribute : Attribute
[SerializableAttribute] [AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Method, AllowMultiple=true)] [ComVisibleAttribute(true)] public ref class ConditionalAttribute sealed : public Attribute

ConditionalAttribute は、Trace クラスおよび Debug クラスで定義される条件付きメソッドをサポートします。
ConditionalAttribute 属性を持つメソッドは常に Microsoft Intermediate Language (MSIL) にコンパイルされますが、これらのメソッドの呼び出しを実行時に行うことはできません。メソッドが引数を持つ場合、実行時に引数の型チェックは行われますが、その評価は行われません。
![]() |
---|
Visual Studio 2005 の既定では、リリース ビルドは定義済みの条件付きコンパイル定数 TRACE を使ってコンパイルされ、デバッグ ビルドは定義済みの DEBUG 定数と TRACE 定数の両方を使ってコンパイルされます。コマンドラインからビルドを実行する場合は、すべての条件付きコンパイル定数を指定する必要があります。 |
![]() |
---|
ConditionString が関連付けられている ConditionalAttribute をメソッドの定義に結びつけ、条件付きメソッドを作成できます。その後、そのメソッドの呼び出しを認識しても、コンパイラはその呼び出しを無視できます。ただし、その呼び出し側でコンパイル変数が定義されていて、その値が ConditionalAttribute に渡された ConditionString と、大文字と小文字も含めて一致しない値であることが条件です。 |
コンパイラでは、そのようなコンパイル変数を定義するために、次に示す複数の手段が用意されています。
-
オペレーティング システムのシェルの環境変数 (SET DEBUG=1 など)。
-
ソース コードのプラグマ (コンパイル変数を定義する #define DEBUG や、同変数を未定義状態にする #undef DEBUG など)。

この属性の使用をサポートしている特定のコンパイラで ConditionalAttribute を使用する方法を次のコンソール アプリケーションの例で示します。
Imports System Imports System.Diagnostics Imports Microsoft.VisualBasic Class Module1 Shared Sub Main() Console.WriteLine("Console.WriteLine is always displayed.") Dim myWriter As New TextWriterTraceListener(System.Console.Out) Debug.Listeners.Add(myWriter) Dim A As New Module1() A.Sub1() End Sub 'Main <Conditional("CONDITION1"), Conditional("CONDITION2")> _ Public Sub Sub1() Sub2() Sub3() End Sub <Conditional("CONDITION1")> _ Public Sub Sub2() Debug.WriteLine("CONDITION1 and DEBUG are defined") End Sub <Conditional("CONDITION2")> _ Public Sub Sub3() Debug.WriteLine("CONDITION2 and DEBUG are defined") Trace.WriteLine("CONDITION2 and TRACE are defined") End Sub End Class ' This console application produces the following output when compiled as shown. ' 'Console.WriteLine is always displayed. ' 'Console.WriteLine is always displayed. 'CONDITION1 and DEBUG are defined ' 'Console.WriteLine is always displayed. 'CONDITION1 and DEBUG are defined 'CONDITION2 and DEBUG are defined
using System; using System.Diagnostics; class Class1 { [STAThread] static void Main(string[] args) { TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out); Debug.Listeners.Add(myWriter); Console.WriteLine("Console.WriteLine is always displayed"); Method1(); Method2(); } [Conditional("CONDITION1")] public static void Method1() { Debug.Write("Method1 - DEBUG and CONDITION1 are specified\n"); Trace.Write("Method1 - TRACE and CONDITION1 are specified\n"); } [Conditional("CONDITION1"), Conditional("CONDITION2")] public static void Method2() { Debug.Write("Method2 - DEBUG, CONDITION1 or CONDITION2 are specified\n"); } } /* This console application produces the following output when compiled as shown. Console.WriteLine is always displayed Method1 - DEBUG and CONDITION1 are specified Method1 - TRACE and CONDITION1 are specified Method2 - DEBUG, CONDITION1 or CONDITION2 are specified Console.WriteLine is always displayed Method2 - DEBUG, CONDITION1 or CONDITION2 are specified Console.WriteLine is always displayed Method1 - TRACE and CONDITION1 are specified */
/* C++ with Managed Extensions uses the C++ standard preprocessor. Use the preprocessor directives rather than this attribute. */

System.Attribute
System.Diagnostics.ConditionalAttribute


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ConditionalAttribute コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)


この属性の使用をサポートしている特定のコンパイラで ConditionalAttribute を使用する方法を次のコンソール アプリケーションの例で示します。
<Conditional("CONDITION1"), Conditional("CONDITION2")> _ Public Sub Sub1() Sub2() Sub3() End Sub <Conditional("CONDITION1")> _ Public Sub Sub2() Debug.WriteLine("CONDITION1 and DEBUG are defined") End Sub <Conditional("CONDITION2")> _ Public Sub Sub3() Debug.WriteLine("CONDITION2 and DEBUG are defined") Trace.WriteLine("CONDITION2 and TRACE are defined") End Sub
[Conditional("CONDITION1")] public static void Method1() { Debug.Write("Method1 - DEBUG and CONDITION1 are specified\n"); Trace.Write("Method1 - TRACE and CONDITION1 are specified\n"); } [Conditional("CONDITION1"), Conditional("CONDITION2")] public static void Method2() { Debug.Write("Method2 - DEBUG, CONDITION1 or CONDITION2 are specified\n"); }
/* C++ with Managed Extensions uses the C++ standard preprocessor. Use the preprocessor directives rather than this attribute. */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ConditionalAttribute プロパティ
ConditionalAttribute メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 ( Attribute から継承されます。) |
![]() | GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 ( Attribute から継承されます。) |
![]() | GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 ( Attribute から継承されます。) |
![]() | GetHashCode | このインスタンスのハッシュ コードを返します。 ( Attribute から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 ( Attribute から継承されます。) |
![]() | IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 ( Attribute から継承されます。) |
![]() | Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 ( Attribute から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

ConditionalAttribute メンバ
指定したプリプロセス識別子を適用する場合に、メソッドが呼び出し可能になることをコンパイラに示します。
ConditionalAttribute データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | ConditionalAttribute | 条件付きメソッドを呼び出すことができるようにする属性の名前を指定して、ConditionalAttribute クラスの新しいインスタンスを初期化します。 |


名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 ( Attribute から継承されます。) |
![]() | GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 (Attribute から継承されます。) |
![]() | GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 (Attribute から継承されます。) |
![]() | GetHashCode | このインスタンスのハッシュ コードを返します。 (Attribute から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。) |
![]() | IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 (Attribute から継承されます。) |
![]() | Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

- ConditionalAttributeのページへのリンク