Switch 類別

定義

提供抽象基底類別,建立新的偵錯與追蹤切換。

public ref class Switch abstract
public abstract class Switch
type Switch = class
Public MustInherit Class Switch
繼承
Switch
衍生

範例

下列範例示範如何使用四個可用來追蹤呼叫堆疊的追蹤層級來定義新的 Switch 類別。 您可以使用 參數來檢測應用程式,以在每次輸入或結束方法時記錄。

第一個範例會建立用來設定參數層級的列舉。

// 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 enum MethodTracingSwitchLevel
{
    Off = 0,
    EnteringMethod = 1,
    ExitingMethod = 2,
    Both = 3,
}
' The following are possible values for the new switch.
Public Enum MethodTracingSwitchLevel
    Off = 0
    EnteringMethod = 1
    ExitingMethod = 2
    Both = 3
End Enum 'MethodTracingSwitchLevel

下列範例會建立新的參數。 程式代碼會實作 Level 屬性來設定新參數的值。 Level 會呼叫受保護的屬性 SwitchSetting ,將值指派給新的參數。 此範例也會實作兩個評估工具屬性,以取得參數的指派值。

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 : 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 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


    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


    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

下列範例會在 中 Main建立新的參數。 它會建立新的參數,併為其指派一個值。 然後,視參數設定而定,它會輸出偵錯訊息以輸入和離開 方法。

public ref class Class1
{
private:

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

public:
    static void main()
    {
        // Add the console listener to see trace messages as console output
        Trace::Listeners->Add(gcnew ConsoleTraceListener(true));
        Debug::AutoFlush = true;

        // Set the switch level to both enter and exit
        mySwitch->Level = MethodTracingSwitchLevel::Both;

        // Write a diagnostic message if the switch is set to entering.
        Debug::WriteLineIf(mySwitch->OutputEnter, "Entering Main");

        // Insert code to handle processing here...

        // Write another diagnostic message if the switch is set to exiting.
        Debug::WriteLineIf(mySwitch->OutputExit, "Exiting Main");
    }
};
public class Class1
{
    /* Create an instance of MyMethodTracingSwitch.*/
    static MyMethodTracingSwitch mySwitch =
        new MyMethodTracingSwitch("Methods", "Trace entering and exiting method");

    public static void Main()
    {
        // Add the console listener to see trace messages as console output
        Trace.Listeners.Add(new ConsoleTraceListener(true));
        Debug.AutoFlush = true;

        // Set the switch level to both enter and exit
        mySwitch.Level = MethodTracingSwitchLevel.Both;

        // Write a diagnostic message if the switch is set to entering.
        Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main");

        // Insert code to handle processing here...

        // Write another diagnostic message if the switch is set to exiting.
        Debug.WriteLineIf(mySwitch.OutputExit, "Exiting 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()
        ' Add the console listener to see trace messages as console output
        Trace.Listeners.Add(New ConsoleTraceListener(True))
        Debug.AutoFlush = True

        ' Set the switch level to both enter and exit
        mySwitch.Level = MethodTracingSwitchLevel.Both

        ' Write a diagnostic message if the switch is set to entering.
        Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main")

        ' Insert code to handle processing here...

        ' Write another diagnostic message if the switch is set to exiting.
        Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main")
    End Sub
End Class

備註

參數提供有效率的機制,可讓您使用外部設定來控制運行時間的追蹤和偵錯輸出。 類別 Switch 會實作參數的預設行為,讓您在運行時間變更交換器層級。

這個類別是、SourceSwitch類別 TraceSwitchBooleanSwitch基類。 這些交換器符合大部分的偵錯和追蹤需求。 如需追蹤參數的詳細資訊,請參閱 追蹤參數

您必須啟用追蹤或偵錯,才能使用參數。 下列語法是編譯程式特有的。 如果您使用 C# 或 Visual Basic 以外的編譯程式,請參閱編譯程式的檔。

  • 若要在 C# 中啟用偵錯,請在編譯程式程式代碼時,將旗標新增 /d:DEBUG 至編譯程式命令行,或者您可以新增 #define DEBUG 至檔案頂端。 在 Visual Basic 中,將 /d:DEBUG=True 旗標新增至編譯程式命令行。

  • 若要在 C# 中使用 啟用追蹤,請在編譯程式程式程式代碼時,將旗標新增 /d:TRACE 至編譯程式命令行,或新增 #define TRACE 至檔案頂端。 在 Visual Basic 中,將 /d:TRACE=True 旗標新增至編譯程式命令行。

若要在 .NET Framework 應用程式中設定參數層級,請編輯對應至應用程式名稱的組態檔。 在此檔案中,您可以新增參數並設定其值、移除參數,或清除應用程式先前設定的所有參數。 組態檔的格式應該如下列範例所示:

<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="true" />  
    </switches>  
  </system.diagnostics>  
</configuration>  

這個範例組態區段會BooleanSwitch定義 屬性設定為 mySwitchDisplayName ,並將 Enabled 值設定為 true。 在您的應用程式中,您可以建立 BooleanSwitch 具有相同名稱的 ,以使用已設定的交換器值,如下列程式代碼範例所示。

private:
    static BooleanSwitch^ boolSwitch = gcnew BooleanSwitch("mySwitch",
        "Switch in config file");

public:
    static void Main( )
    {
        //...
        Console::WriteLine("Boolean switch {0} configured as {1}",
            boolSwitch->DisplayName, ((Boolean^)boolSwitch->Enabled)->ToString());
        if (boolSwitch->Enabled)
        {
            //...
        }
    }
private static BooleanSwitch boolSwitch = new BooleanSwitch("mySwitch",
    "Switch in config file");

public static void Main()
{
    //...
    Console.WriteLine("Boolean switch {0} configured as {1}",
        boolSwitch.DisplayName, boolSwitch.Enabled.ToString());
    if (boolSwitch.Enabled)
    {
        //...
    }
}

給實施者的注意事項

如果您需要追蹤層級,或設定與、 和所提供的BooleanSwitch參數層級不同的機制,您可以從 繼承自 SwitchSourceSwitchTraceSwitch 繼承自這個類別時,您必須實作 SwitchSetting 方法。

建構函式

Switch(String, String)

初始化 Switch 類別的新執行個體。

Switch(String, String, String)

指定的參數顯示名稱、描述和預設值,初始化 Switch 類別的新執行個體。

屬性

Attributes

取得在應用程式組態檔中定義的自訂參數屬性。

DefaultValue

取得建構函式中指派的預設值。

Description

取得切換控制的描述。

DisplayName

取得用來識別切換控制的名稱。

SwitchSetting

取得或設定這個切換控制的目前設定。

Value

取得或設定參數的值。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetSupportedAttributes()

取得由參數支援的自訂屬性。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnSwitchSettingChanged()

SwitchSetting 屬性變更時叫用。

OnValueChanged()

Value 屬性變更時叫用。

Refresh()

重新整理追蹤組態資料。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

事件

Initializing

發生於需要初始化 時 Switch

適用於

另請參閱