AppContext.TryGetSwitch(String, Boolean) Yöntem

Tanım

Bir anahtarın değerini almaya çalışır.

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

Parametreler

switchName
String

Anahtarın adı.

isEnabled
Boolean

Bu yöntem döndürdüğünde, bulunduysa switchName veya false bulunamadıysa switchName değerini switchName içerir. Bu parametre, başlatmadan iletilir.

Döndürülenler

true ayarlanmışsa switchName ve isEnabled bağımsız değişken anahtarın değerini içeriyorsa; değilse, false.

Özel durumlar

switchName, null değeridir.

switchName, Empty değeridir.

Örnekler

Aşağıdaki örnek, bir kitaplık tüketicisinin adlı Switch.AmazingLib.ThrowOnExceptionbir anahtar ayarlayıp ayarlamadığını belirler.

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

Açıklamalar

sınıfı, AppContext kitaplık yazarlarının kullanıcıları için yeni işlevler için tekdüzen bir geri çevirme mekanizması sağlamasına olanak tanır. Bir geri çevirme isteğini iletmek için bileşenler arasında gevşek bir şekilde bağlanmış bir sözleşme oluşturur. Bu özellik, mevcut işlevsellikte bir değişiklik yapıldığında genellikle önemlidir. Buna karşılık, yeni işlevler için zaten örtük bir katılım vardır.

Ortak dil çalışma zamanı, kayıt defterini ve uygulamanın yapılandırma dosyasını okuyarak bir AppContext örneğe atanan anahtarları otomatik olarak doldurur. Bu anahtarların değeri daha sonra geçersiz kılınabilir ve yöntemi çağrılarak SetSwitch yeni anahtarlar eklenebilir.

Kitaplık, tüketicilerinin anahtarın TryGetSwitch değerini bildirip bildirmediğini denetlemek için yöntemini çağırır ve ardından bu anahtar üzerinde uygun şekilde hareket eder. Varsayılan olarak, anahtar tanımlanmamışsa yeni işlevsellik etkinleştirilir. Anahtar tanımlanmışsa ve değeri ise false, yeni işlevsellik de etkinleştirilir. Değeri ise true, eski davranış etkinleştirilir.

Şunlara uygulanır

Ayrıca bkz.