ConditionalAttributeとは? わかりやすく解説

ConditionalAttribute クラス

指定したプリプロセス識別子を適用する場合に、メソッドが呼び出し可能になることをコンパイラに示します。

名前空間: System.Diagnostics
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Method,
 AllowMultiple:=True)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class
 ConditionalAttribute
    Inherits Attribute
Dim instance As ConditionalAttribute
[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
/** @attribute SerializableAttribute() */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method,
 AllowMultiple=true) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class ConditionalAttribute extends
 Attribute
SerializableAttribute 
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple=true)
 
ComVisibleAttribute(true) 
public final class ConditionalAttribute extends
 Attribute
解説解説

ConditionalAttribute は、Trace クラスおよび Debug クラスで定義される条件付きメソッドをサポートします。

ConditionalAttribute 属性を持つメソッドは常に Microsoft Intermediate Language (MSIL) にコンパイルされますが、これらのメソッドの呼び出しを実行時に行うことはできません。メソッドが引数を持つ場合、実行時に引数の型チェックは行われますが、その評価は行われません。

メモメモ

ConditionString が関連付けられている ConditionalAttribute をメソッドの定義に結びつけ、条件付きメソッドを作成できます。その後、そのメソッドの呼び出しを認識しても、コンパイラはその呼び出しを無視できます。ただし、その呼び出し側でコンパイル変数が定義されていて、その値が ConditionalAttribute に渡された ConditionString と、大文字と小文字も含めて一致しない値であることが条件です。

コンパイラでは、そのようなコンパイル変数を定義するために、次に示す複数の手段が用意されています。

CLS 準拠のコンパイラでは、ConditionalAttribute を無視できます。

属性の使用方法については、「属性を使用したメタデータの拡張」を参照してください。

使用例使用例

この属性の使用をサポートしている特定のコンパイラで 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.Object
   System.Attribute
    System.Diagnostics.ConditionalAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

ConditionalAttribute コンストラクタ

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

名前空間: System.Diagnostics
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    conditionString As String _
)
Dim conditionString As String

Dim instance As New ConditionalAttribute(conditionString)
public ConditionalAttribute (
    string conditionString
)
public:
ConditionalAttribute (
    String^ conditionString
)
public ConditionalAttribute (
    String conditionString
)
public function ConditionalAttribute (
    conditionString : String
)

パラメータ

conditionString

現在のインスタンスの対象メソッドを呼び出し可能にするプリプロセス識別子を含む文字列。

使用例使用例

この属性の使用をサポートしている特定のコンパイラで 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.
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ConditionalAttribute クラス
ConditionalAttribute メンバ
System.Diagnostics 名前空間

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 クラス
System.Diagnostics 名前空間

ConditionalAttribute メンバ

指定したプリプロセス識別子を適用する場合に、メソッドが呼び出し可能になることをコンパイラに示します。

ConditionalAttribute データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド ConditionalAttribute 条件付きメソッドを呼び出すことができるようにする属性の名前を指定して、ConditionalAttribute クラスの新しいインスタンスを初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ .NET Compact Framework によるサポート TypeId  派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。(Attribute から継承されます。)
パブリック メソッドパブリック メソッド
  名前 説明
パブリック メソッド 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 クラス
System.Diagnostics 名前空間



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「ConditionalAttribute」の関連用語

ConditionalAttributeのお隣キーワード

   

英語⇒日本語
日本語⇒英語
   



ConditionalAttributeのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS