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 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からConditionalAttributeを検索した結果を表示しています。
Weblioに収録されているすべての辞書からConditionalAttributeを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からConditionalAttribute を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ConditionalAttribute」の関連用語

ConditionalAttributeのお隣キーワード
検索ランキング

   

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



ConditionalAttributeのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS