Switch Sınıf

Tanım

Yeni hata ayıklama ve izleme anahtarları oluşturmak için soyut bir temel sınıf sağlar.

public ref class Switch abstract
public abstract class Switch
type Switch = class
Public MustInherit Class Switch
Devralma
Switch
Türetilmiş

Örnekler

Aşağıdaki örnekte, bir çağrı yığınını izlemek için kullanılabilecek dört izleme düzeyine sahip yeni Switch bir sınıfın nasıl tanımlanacağı gösterilmektedir. Yöntem her girildiğinde veya yöntemden çıkıldığında uygulamanızı günlüğe kaydetmesini sağlamak için anahtarını kullanabilirsiniz.

İlk örnek, anahtarın düzeyini ayarlamak için kullanılan numaralandırmayı oluşturur.

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

Aşağıdaki örnek yeni anahtarı oluşturur. Kod, yeni anahtarın değerini ayarlamak için bir Level özellik uygular. Level değeri yeni anahtara atayan protected özelliğini SwitchSetting çağırır. Bu örnek ayrıca anahtarın atanan değerini almak için iki değerlendirici özelliği uygular.

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

Aşağıdaki örnek içinde Mainyeni bir anahtar oluşturur. Yeni bir anahtar oluşturur ve ona bir değer atar. Ardından, anahtar ayarlarına bağlı olarak, yöntemi girmek ve bırakmak için hata ayıklama iletilerinin çıkışını alır.

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

Açıklamalar

Anahtar, dış ayarları kullanarak çalışma zamanında izleme ve hata ayıklama çıkışını denetlemek için verimli bir mekanizma sağlar. sınıfı Switch anahtarlar için varsayılan davranışı uygulayarak çalışma zamanında anahtar düzeyini değiştirmenize olanak sağlar.

Bu sınıf, , SourceSwitchve TraceSwitch sınıfları için BooleanSwitchtemel sınıftır. Bu anahtarlar çoğu hata ayıklama ve izleme gereksinimlerini karşılar. İzleme anahtarları hakkında daha fazla bilgi için bkz . İzleme Anahtarları.

Anahtar kullanmak için izlemeyi veya hata ayıklamayı etkinleştirmeniz gerekir. Aşağıdaki söz dizimi derleyiciye özgüdür. C# veya Visual Basic dışında derleyiciler kullanıyorsanız, derleyicinizin belgelerine bakın.

  • C# dilinde hata ayıklamayı etkinleştirmek için, /d:DEBUG kodunuzu derlerken derleyici komut satırına bayrağını ekleyin veya dosyanızın en üstüne ekleyebilirsiniz #define DEBUG . Visual Basic'te bayrağını /d:DEBUG=True derleyici komut satırına ekleyin.

  • C# dilinde kullanarak izlemeyi etkinleştirmek için, /d:TRACE kodunuzu derlerken derleyici komut satırına bayrağını ekleyin veya dosyanızın en üstüne ekleyin #define TRACE . Visual Basic'te bayrağını /d:TRACE=True derleyici komut satırına ekleyin.

.NET Framework bir uygulamada anahtarınızın düzeyini ayarlamak için uygulamanızın adına karşılık gelen yapılandırma dosyasını düzenleyin. Bu dosyanın içinde bir anahtar ekleyebilir ve değerini ayarlayabilir, bir anahtarı kaldırabilir veya uygulama tarafından daha önce ayarlanan tüm anahtarları temizleyebilirsiniz. Yapılandırma dosyası aşağıdaki örnekte olduğu gibi biçimlendirilmelidir:

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

Bu örnek yapılandırma bölümü, özelliğinin ve değerinin EnabledDisplayName olarak ayarlandığı truemySwitch bir BooleanSwitch öğesini tanımlar. Uygulamanızın içinde, aşağıdaki kod örneğinde gösterildiği gibi aynı ada sahip bir BooleanSwitch oluşturarak yapılandırılmış anahtar değerini kullanabilirsiniz.

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)
    {
        //...
    }
}

Uygulayanlara Notlar

İzleme düzeylerine veya ve tarafından BooleanSwitchSourceSwitchTraceSwitchsağlananlardan farklı anahtar düzeylerini ayarlamak için mekanizmalara ihtiyacınız varsa, 'den Switchdevralabilirsiniz. Bu sınıftan devralırken yöntemini uygulamanız SwitchSetting gerekir.

Oluşturucular

Switch(String, String)

Switch sınıfının yeni bir örneğini başlatır.

Switch(String, String, String)

Anahtarın Switch görünen adını, açıklamasını ve varsayılan değerini belirterek sınıfın yeni bir örneğini başlatır.

Özellikler

Attributes

Uygulama yapılandırma dosyasında tanımlanan özel anahtar özniteliklerini alır.

DefaultValue

Oluşturucuda atanan varsayılan değeri alır.

Description

Anahtarın açıklamasını alır.

DisplayName

Anahtarı tanımlamak için kullanılan bir ad alır.

SwitchSetting

Bu anahtar için geçerli ayarı alır veya ayarlar.

Value

Anahtarın değerini alır veya ayarlar.

Yöntemler

Equals(Object)

Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.

(Devralındığı yer: Object)
GetHashCode()

Varsayılan karma işlevi işlevi görür.

(Devralındığı yer: Object)
GetSupportedAttributes()

Anahtar tarafından desteklenen özel öznitelikleri alır.

GetType()

Type Geçerli örneğini alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
OnSwitchSettingChanged()

Özellik değiştirildiğinde SwitchSetting çağrılır.

OnValueChanged()

Özellik değiştirildiğinde Value çağrılır.

Refresh()

İzleme yapılandırma verilerini yeniler.

ToString()

Geçerli nesneyi temsil eden dizeyi döndürür.

(Devralındığı yer: Object)

Ekinlikler

Initializing

Başlatılması Switch gerektiğinde gerçekleşir.

Şunlara uygulanır

Ayrıca bkz.