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 クラスのページへのリンク