Share via


AppContext.TryGetSwitch(String, Boolean) Metode

Definisi

Mencoba untuk mendapatkan nilai sakelar.

public:
 static bool TryGetSwitch(System::String ^ switchName, [Runtime::InteropServices::Out] bool % isEnabled);
public static bool TryGetSwitch (string switchName, out bool isEnabled);
static member TryGetSwitch : string * bool -> bool
Public Shared Function TryGetSwitch (switchName As String, ByRef isEnabled As Boolean) As Boolean

Parameter

switchName
String

Nama sakelar.

isEnabled
Boolean

Ketika metode ini kembali, berisi nilai switchName jika switchName ditemukan, atau false jika switchName tidak ditemukan. Parameter ini diteruskan tanpa diinisialisasi.

Mengembalikan

true jika switchName diatur dan isEnabled argumen berisi nilai sakelar; jika tidak, false.

Pengecualian

switchNameadalah null.

switchNameadalah Empty.

Contoh

Contoh berikut menentukan apakah konsumen pustaka telah menetapkan sakelar bernama Switch.AmazingLib.ThrowOnException.

public class AmazingLib
{
   private bool shouldThrow;

   public void PerformAnOperation()
   {
      if (!AppContext.TryGetSwitch("Switch.AmazingLib.ThrowOnException", out shouldThrow)) {
         // This is the case where the switch value was not set by the application.
         // The library can choose to get the value of shouldThrow by other means.
         // If no overrides or default values are specified, the value should be 'false'.
         // A false value implies the latest behavior.
      }

      // The library can use the value of shouldThrow to throw exceptions or not.
      if (shouldThrow) {
         // old code
      }
      else {
          // new code
      }
   }
}
module AmazingLib =
    let performAnOperation () =
        match AppContext.TryGetSwitch "Switch.AmazingLib.ThrowOnException" with
        | false, _ ->
            // This is the case where the switch value was not set by the application.
            // The library can choose to get the value of shouldThrow by other means.
            // If no overrides or default values are specified, the value should be 'false'.
            // A false value implies the latest behavior.
            ()
        | true, shouldThrow ->
            // The library can use the value of shouldThrow to throw exceptions or not.
            if shouldThrow then
                // old code
                ()
            else
                // new code
                ()
Public Class AmazingLib

   Private shouldThrow As Boolean

   Public Sub PerformAnOperation()
      If Not AppContext.TryGetSwitch("Switch.AmazingLib.ThrowOnException", shouldThrow) Then 
         ' This is the case where the switch value was not set by the application. 
         ' The library can choose to get the value of shouldThrow by other means. 
         ' If no overrides or default values are specified, the value should be 'false'. 
         ' A false value implies the latest behavior.
      End If

      ' The library can use the value of shouldThrow to throw exceptions or not.
      If shouldThrow Then
         ' old code
      Else 
          ' new code
      End If
   End Sub
End Class

Keterangan

Kelas ini AppContext memungkinkan penulis pustaka untuk menyediakan mekanisme penolakan seragam untuk fungsionalitas baru bagi pengguna mereka. Ini menetapkan kontrak yang digabungkan secara longgar antara komponen untuk mengkomunikasikan permintaan penolakan. Kemampuan ini biasanya penting ketika perubahan dilakukan pada fungsionalitas yang ada. Sebaliknya, sudah ada keikutsertaan implisit untuk fungsionalitas baru.

Runtime bahasa umum secara otomatis mengisi sakelar yang AppContext ditetapkan ke instans dengan membaca registri dan file konfigurasi aplikasi. Nilai sakelar ini kemudian dapat ditimpa, dan sakelar baru ditambahkan, dengan memanggil SetSwitch metode .

Pustaka memanggil TryGetSwitch metode untuk memeriksa apakah konsumennya telah menyatakan nilai sakelar dan kemudian bertindak dengan tepat di atasnya. Secara default, jika sakelar tidak ditentukan, fungsionalitas baru diaktifkan. Jika sakelar ditentukan dan nilainya adalah false, fungsionalitas baru juga diaktifkan. Jika nilainya adalah true, perilaku warisan diaktifkan.

Berlaku untuk

Lihat juga