Switch クラス
アセンブリ: System (system.dll 内)
構文
解説
スイッチは、外部設定を使用して、実行時にトレース出力やデバッグ出力を制御するための効率的な機構を提供します。Switch クラスは、スイッチの既定の動作を実装します。スイッチ レベルは、実行時に変更できます。
このクラスは、BooleanSwitch、SourceSwitch、および TraceSwitch の各クラスの基本クラスです。これらのスイッチで、ほとんどのデバッグおよびトレースの要件を満たすことができます。固有のスイッチを作成する場合は、static にする必要があります。
スイッチを使用するには、トレースまたはデバッグを有効にする必要があります。次の構文はコンパイラに固有です。C# または Visual Basic 以外のコンパイラを使用する場合は、使用するコンパイラのドキュメントを参照してください。
-
C# でデバッグを有効にするには、コードのコンパイル時に /d:DEBUG フラグをコンパイラのコマンド ラインに追加するか、#define DEBUG をファイルの最上部に挿入します。Visual Basic では、コンパイラのコマンド ラインに /d:DEBUG=True フラグを追加します。
-
C# でトレースを有効にするには、コードのコンパイル時に /d:TRACE フラグをコンパイラのコマンド ラインに追加するか、#define TRACE をファイルの最上部に挿入します。Visual Basic では、コンパイラのコマンド ラインに /d:TRACE=True フラグを追加します。
スイッチのレベルを設定するには、アプリケーションの名前に対応する構成ファイルを編集します。このファイルでは、スイッチの追加、その値の設定、スイッチの削除、アプリケーションで以前設定されたすべてのスイッチのクリアを実行できます。構成ファイルの書式は次の例のようになります。
<configuration> <system.diagnostics> <switches> <add name="mySwitch" value="10" /> <add name="myNewSwitch" value="20" /> <remove name="mySwitch" /> <clear/> </switches> </system.diagnostics> </configuration>
メモ |
---|
コール スタックのトレースに使用できる 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.Diagnostics.Switch
System.Diagnostics.BooleanSwitch
System.Diagnostics.SourceSwitch
System.Diagnostics.TraceSwitch
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Switch コンストラクタ (String, String)
アセンブリ: System (system.dll 内)
構文
Protected Sub New ( _ displayName As String, _ description As String _ )
Dim displayName As String Dim description As String Dim instance As New Switch(displayName, description)
protected Switch ( string displayName, string description )
protected: Switch ( String^ displayName, String^ description )
protected Switch ( String displayName, String description )
protected function Switch ( displayName : String, description : String )
- displayName
スイッチの名前。
新しい Switch オブジェクトを作成するときに、displayName パラメータの値を使用して、スイッチの初期設定値を検索します。既定値は空の文字列 ("") です。
XML 構成ファイルでは、スイッチの追加、その値の設定、スイッチの削除、アプリケーションで以前設定されたすべてのスイッチのクリアを実行できます。アプリケーションの実行中は、構成ファイルへの動的な変更は検出されません。構成ファイルへの変更を有効化するには、アプリケーションを停止して再起動する必要があります。構成ファイルの書式は次の例のようになります。
<configuration> <system.diagnostics> <switches> <add name="mySwitch" value="10" /> <add name="myNewSwitch" value="20" /> <remove name="mySwitch" /> <clear/> </switches> </system.diagnostics> </configuration>
メモ |
---|
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Switch コンストラクタ
オーバーロードの一覧
名前 | 説明 |
---|---|
Switch (String, String) | Switch クラスの新しいインスタンスを初期化します。 |
Switch (String, String, String) | スイッチの表示名、説明、および既定値を指定して、Switch クラスの新しいインスタンスを初期化します。 |
Switch コンストラクタ (String, String, String)
アセンブリ: 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
スイッチの名前。
displayName パラメータは DisplayName プロパティの値を設定するために使用され、description パラメータは Description プロパティの値を設定するために使用されます。defaultSwitchValue パラメータは、Value コードまたは構成ファイルの属性によってプロパティが設定されない場合のスイッチの値です。詳細については、Switch(String,String) オーバーロードのトピックを参照してください。
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Switch プロパティ
パブリック プロパティ
名前 | 説明 | |
---|---|---|
Attributes | アプリケーション構成ファイルに定義されているカスタム スイッチ属性を取得します。 | |
Description | スイッチの説明を取得します。 | |
DisplayName | スイッチを識別するための名前を取得します。 |
参照
Switch メソッド
パブリック メソッド
名前 | 説明 | |
---|---|---|
Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) | |
GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) | |
GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) | |
ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) | |
ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |
名前 | 説明 | |
---|---|---|
Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) | |
GetSupportedAttributes | スイッチによってサポートされるカスタム属性を取得します。 | |
MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) | |
OnSwitchSettingChanged | SwitchSetting プロパティが変更されると発生します。 | |
OnValueChanged | Value プロパティが変更されると発生します。 |
Switch メンバ
新しいデバッグ スイッチおよびトレース スイッチを作成する abstract 基本クラスを提供します。
Switch データ型で公開されるメンバを以下の表に示します。
プロテクト コンストラクタ
パブリック プロパティ
名前 | 説明 | |
---|---|---|
Attributes | アプリケーション構成ファイルに定義されているカスタム スイッチ属性を取得します。 | |
Description | スイッチの説明を取得します。 | |
DisplayName | スイッチを識別するための名前を取得します。 |
パブリック メソッド
名前 | 説明 | |
---|---|---|
Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) | |
GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) | |
GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) | |
ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) | |
ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |
名前 | 説明 | |
---|---|---|
Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) | |
GetSupportedAttributes | スイッチによってサポートされるカスタム属性を取得します。 | |
MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) | |
OnSwitchSettingChanged | SwitchSetting プロパティが変更されると発生します。 | |
OnValueChanged | Value プロパティが変更されると発生します。 |
固有名詞の分類
Weblioに収録されているすべての辞書からSwitchを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からSwitchを検索
- Switchのページへのリンク