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

Switch クラス

新しデバッグ スイッチおよびトレース スイッチ作成する abstract 基本クラス提供します

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

解説解説

スイッチは、外部設定使用して実行時トレース出力デバッグ出力制御するための効率的な機構提供しますSwitch クラスは、スイッチ既定動作実装ます。スイッチ レベルは、実行時変更できます

このクラスは、BooleanSwitch、SourceSwitch、および TraceSwitch の各クラス基本クラスです。これらのスイッチで、ほとんどのデバッグおよびトレース要件満たすことができます固有のスイッチ作成する場合は、static にする必要があります

スイッチ使用するには、トレースまたはデバッグ有効にする必要があります次の構文コンパイラ固有です。C# または Visual Basic 以外のコンパイラ使用する場合は、使用するコンパイラドキュメント参照してください

スイッチレベル設定するには、アプリケーションの名前に対応する構成ファイル編集します。このファイルでは、スイッチ追加、その値の設定スイッチ削除アプリケーション以前設定されすべてのスイッチクリア実行できます構成ファイル書式次の例のようになります

<configuration>
  <system.diagnostics>
    <switches>
      <add name="mySwitch" value="10" />
      <add name="myNewSwitch" value="20" />
      <remove name="mySwitch" />
      <clear/>
    </switches>
  </system.diagnostics>
</configuration>
メモメモ

クラスSwitchメンバstatic にすることで、パフォーマンス向上させることができます

継承時の注意 トレース レベルや、BooleanSwitchSourceSwitch、および TraceSwitch によって提供されるものとは異なスイッチ レベル設定するための機構必要な場合は、Switch から継承します。このクラスから継承するときは、SwitchSetting メソッド実装する必要があります

使用例使用例

コール スタックトレース使用できる 4 段階のトレース レベルを持つ新しSwitch クラス定義する方法の例を次に示します。このスイッチ使用すると、メソッド呼び出されるたび、またはメソッドから制御が戻るたびに、アプリケーションログ作成できます

最初の例では、スイッチレベル設定するために使用する列挙体を作成します

' The following are possible values for the new switch.
Public Enum MethodTracingSwitchLevel
    Off = 0
    EnteringMethod = 1
    ExitingMethod = 2
    Both = 3
End Enum 'MethodTracingSwitchLevel

// The following are possible values for the new switch.
 public enum MethodTracingSwitchLevel {
    Off = 0,
    EnteringMethod = 1,
    ExitingMethod = 2,
    Both = 3,
 }
// The following are possible values for the new switch.
public enum class MethodTracingSwitchLevel
{
   Off = 0,
   EnteringMethod = 1,
   ExitingMethod = 2,
   Both = 3
};


// The following are possible values for the new switch.
public class MethodTracingSwitchLevel
{
    private int member;

    MethodTracingSwitchLevel()
    {
        member = 0;
    }//MethodTracingSwitchLevel

    MethodTracingSwitchLevel(int n)
    {
        member = n;
    }//MethodTracingSwitchLevel

    public int get_Member()
    {
        return member;
    }//get_Member

    public static int off
 = 0;
    public static int enteringMethod
 = 1;
    public static int exitingMethod
 = 2;
    public static int both
 = 3;
} //MethodTracingSwitchLevel

新しスイッチ作成する例を次に示します。このコードは、新しスイッチの値を設定する Level プロパティ実装ます。Level は、新しスイッチに値を割り当てるプロテクト プロパティ SwitchSetting呼び出します。また、この例では、スイッチ代入した値を取得する 2 つアクセサ プロパティ実装ます。

Public Class MyMethodTracingSwitch
    Inherits Switch
    Protected outExit As Boolean
    Protected outEnter As Boolean
    Protected myLevel As MethodTracingSwitchLevel
    
    
    Public Sub New(displayName
 As String, description As
 String)
        MyBase.New(displayName, description)
    End Sub 'New
    
    
    Public Property Level() As
 MethodTracingSwitchLevel
        Get
            Return myLevel
        End Get
        Set
            SetSwitchSetting(CInt(value))
        End Set
    End Property
    
    
    Protected Sub SetSwitchSetting(value As
 Integer)
        If value < 0 Then
            value = 0
        End If
        If value > 3 Then
            value = 3
        End If
        
        myLevel = CType(value, MethodTracingSwitchLevel)
        
        outEnter = False
        If value = CInt(MethodTracingSwitchLevel.EnteringMethod)
 Or _
            value = CInt(MethodTracingSwitchLevel.Both) Then
            
            outEnter = True
        End If
        
        outExit = False
        If value = CInt(MethodTracingSwitchLevel.ExitingMethod)
 Or _
            value = CInt(MethodTracingSwitchLevel.Both) Then
            
            outExit = True
        End If
    End Sub 'SetSwitchSetting
    
    
    Public ReadOnly Property
 OutputExit() As Boolean
        Get
            Return outExit
        End Get
    End Property
    
    
    Public ReadOnly Property
 OutputEnter() As Boolean
        Get
            Return outEnter
        End Get
    End Property
End Class 'MyMethodTracingSwitch

public class MyMethodTracingSwitch:Switch {
     protected bool outExit;
     protected bool outEnter;
     protected MethodTracingSwitchLevel level;
 
     public MyMethodTracingSwitch(string displayName,
 string description):base(displayName, description){
     }
 
     public MethodTracingSwitchLevel Level {
         get{
             return level;
         }
         set{
             SetSwitchSetting((int)value);
         }
     }
     
     protected void SetSwitchSetting(int
 value){
         if(value<0){
             value = 0;
         }
         if(value>3){
             value = 3;
         }
 
         level = (MethodTracingSwitchLevel)value;
 
         outEnter = false;
         if((value == (int)MethodTracingSwitchLevel.EnteringMethod)
 || (value == (int)MethodTracingSwitchLevel.Both)){
             outEnter = true;
         }
 
         outExit = false;
         if((value == (int)MethodTracingSwitchLevel.ExitingMethod)
 || (value == (int)MethodTracingSwitchLevel.Both)){
             outExit = true;
         }
     }
 
     public bool OutputExit{
         get{
             return outExit;
         }
     }
 
     public bool OutputEnter{
         get{
             return outEnter;
         }
     }
 }
public ref class MyMethodTracingSwitch: public
 Switch
{
protected:
   bool outExit;
   bool outEnter;
   MethodTracingSwitchLevel level;

public:
   MyMethodTracingSwitch( String^ displayName, String^ description )
      : Switch( displayName, description )
   {}


   property MethodTracingSwitchLevel Level 
   {
      MethodTracingSwitchLevel get()
      {
         return level;
      }

      void set( MethodTracingSwitchLevel value
 )
      {
         SetSwitchSetting( (int)value );
      }

   }

protected:
   void SetSwitchSetting( int value )
   {
      if ( value < 0 )
      {
         value = 0;
      }

      if ( value > 3 )
      {
         value = 3;
      }

      level = (MethodTracingSwitchLevel)value;
      outEnter = false;
      if ( (value == (int)MethodTracingSwitchLevel::EnteringMethod)
 || (value == (int)MethodTracingSwitchLevel::Both) )
      {
         outEnter = true;
      }

      outExit = false;
      if ( (value == (int)MethodTracingSwitchLevel::ExitingMethod)
 || (value == (int)MethodTracingSwitchLevel::Both) )
      {
         outExit = true;
      }
   }


public:

   property bool OutputExit 
   {
      bool get()
      {
         return outExit;
      }

   }

   property bool OutputEnter 
   {
      bool get()
      {
         return outEnter;
      }

   }

};


public class MyMethodTracingSwitch extends
 Switch
{
    protected boolean outExit;
    protected boolean outEnter;
    protected MethodTracingSwitchLevel level;

    public MyMethodTracingSwitch(String displayName, String description)
    {
      super(displayName, description);
    } //MyMethodTracingSwitch

    /** @property
     */
    public MethodTracingSwitchLevel get_Level()
    {
        return level;
    }//get_Level

    /** @property 
     */
    public void set_Level(MethodTracingSwitchLevel
 value)
    {
        SetSwitchSetting(value.get_Member());
    }//set_Level

    protected void SetSwitchSetting(int
 value)
    {
        if (value < 0) {
            value = 0;
        }
        if (value > 3) {
            value = 3;
        }

        level = new MethodTracingSwitchLevel(value);
        outEnter = false;
        if (value == (int)(MethodTracingSwitchLevel.enteringMethod)
 
            || value == (int)(MethodTracingSwitchLevel.both))
 {
            outEnter = true;
        }

        outExit = false;
        if (value == (int)(MethodTracingSwitchLevel.exitingMethod)
 
            || value == (int)(MethodTracingSwitchLevel.both))
 {
            outExit = true;
        }
    } //SetSwitchSetting

    /** @property 
     */
    public boolean get_OutputExit()
    {
        return outExit;
    }//get_OutputExit

    /** @property 
     */
    public boolean get_OutputEnter()
    {
        return outEnter;
    }//get_OutputEnter
} //MyMethodTracingSwitch

新しMain作成する例を次に示します。この例では、新しスイッチ作成し、値を割り当てますその後スイッチ設定に応じてメソッド呼び出しおよび終了についてのデバッグ メッセージ出力します

Public Class Class1
    ' Create an instance of MyMethodTracingSwitch. 
    Private Shared mySwitch As
 New _
        MyMethodTracingSwitch("Methods", "Trace
 entering and exiting method")
    
    Public Shared Sub Main()
        ' Write a diagnostic message if the switch is set to entering.
        Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main")
        
        ' Insert code to handle processing.
        ' Write another diagnostic message if the switch is set to exiting.
        Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main")
    End Sub
End Class 'MyClass
public class MyClass {
    /* Create an instance of MyMethodTracingSwitch.*/
    static MyMethodTracingSwitch mySwitch = 
       new MyMethodTracingSwitch("Methods", "Trace
 entering and exiting method");
 
    public static int Main(string[]
 args) {
       // Write a diagnostic message if the switch is set to entering.
       Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main");
 
       // Insert code to handle processing.
 
       // Write another diagnostic message if the switch is set to exiting.
       Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main");
       return 0;
    }
 }
public ref class MyClass
{
private:

   /* Create an instance of MyMethodTracingSwitch.*/
   static MyMethodTracingSwitch^ mySwitch = gcnew MyMethodTracingSwitch(
 "Methods","Trace entering and exiting method" );

public:
   static int main()
   {
      
      // Write a diagnostic message if the switch is set to entering.
      Debug::WriteLineIf( mySwitch->OutputEnter, "Entering Main" );
      
      // Insert code to handle processing.
      // Write another diagnostic message if the switch is set to exiting.
      Debug::WriteLineIf( mySwitch->OutputExit, "Exiting Main" );
      return 0;
   }

};


public class MyClass
{
    /* Create an instance of MyMethodTracingSwitch.
    */
    private static MyMethodTracingSwitch mySwitch
 = new MyMethodTracingSwitch(
        "Methods", "Trace entering and exiting method");

    public static void main(String[]
 args)
    {
        // Write a diagnostic message if the switch is set to entering.
        Debug.WriteLineIf(mySwitch.get_OutputEnter(), "Entering main");
        // Insert code to handle processing.
        // Write another diagnostic message if the switch is set to
 exiting.
        Debug.WriteLineIf(mySwitch.get_OutputExit(), "Exiting main");
        return;
    } //main
} //MyClass
継承階層継承階層
System.Object
  System.Diagnostics.Switch
     System.Diagnostics.BooleanSwitch
     System.Diagnostics.SourceSwitch
     System.Diagnostics.TraceSwitch
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Switch コンストラクタ (String, String)

Switch クラス新しインスタンス初期化します。

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

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Switch コンストラクタ


Switch コンストラクタ (String, String, String)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

スイッチ表示名説明、および既定値指定して、Switch クラス新しインスタンス初期化します。

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

Protected Sub New ( _
    displayName As String, _
    description As String, _
    defaultSwitchValue As String _
)
Dim displayName As String
Dim description As String
Dim defaultSwitchValue As String

Dim instance As New Switch(displayName,
 description, defaultSwitchValue)
protected Switch (
    string displayName,
    string description,
    string defaultSwitchValue
)
protected:
Switch (
    String^ displayName, 
    String^ description, 
    String^ defaultSwitchValue
)
protected Switch (
    String displayName, 
    String description, 
    String defaultSwitchValue
)
protected function Switch (
    displayName : String, 
    description : String, 
    defaultSwitchValue : String
)

パラメータ

displayName

スイッチの名前。

description

スイッチ説明

defaultSwitchValue

スイッチ既定値

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Switch プロパティ


Switch メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

Switch クラス
System.Diagnostics 名前空間
BooleanSwitch クラス
TraceSwitch
Debug クラス
Trace

Switch メンバ

新しデバッグ スイッチおよびトレース スイッチ作成する abstract 基本クラス提供します

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


プロテクト コンストラクタプロテクト コンストラクタ
パブリック プロパティパブリック プロパティ
プロテクト プロパティプロテクト プロパティ
  名前 説明
プロテクト プロパティ SwitchSetting このスイッチ現在の設定取得または設定します
プロテクト プロパティ Value スイッチの値を取得または設定します
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

Switch クラス
System.Diagnostics 名前空間
BooleanSwitch クラス
TraceSwitch
Debug クラス
Trace




固有名詞の分類

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

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

辞書ショートカット

すべての辞書の索引

「Switch」の関連用語

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

   

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



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

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

©2024 GRAS Group, Inc.RSS