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) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


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

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

辞書ショートカット

すべての辞書の索引

「ConditionalAttribute クラス」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS