AppContext.TryGetSwitch(String, Boolean) Metodo

Definizione

Prova a ottenere il valore di un'opzione.

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

Parametri

switchName
String

Nome dell'opzione.

isEnabled
Boolean

Quando questo metodo viene restituito, contiene il valore di switchName se switchName è stato trovato oppure false se switchName non è stato trovato. Questo parametro viene passato non inizializzato.

Restituisce

true se switchName è stato impostato e l'argomento isEnabled contiene il valore dell'opzione; in caso contrario, false.

Eccezioni

switchName è null.

switchName è Empty.

Esempio

Nell'esempio seguente viene determinato se un consumer di libreria ha impostato un commutatore denominato 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

Commenti

La AppContext classe consente ai writer della libreria di fornire un meccanismo di consenso esplicito uniforme per le nuove funzionalità per gli utenti. Stabilisce un contratto associato in modo libero tra i componenti per comunicare una richiesta di rifiuto esplicito. Questa funzionalità è importante in genere quando viene apportata una modifica alle funzionalità esistenti. Al contrario, esiste già un consenso esplicito per la nuova funzionalità.

Common Language Runtime popola automaticamente le opzioni assegnate a un'istanza AppContext leggendo il Registro di sistema e il file di configurazione dell'applicazione. Il valore di queste opzioni può quindi essere sottoposto a override e le nuove opzioni aggiunte chiamando il SetSwitch metodo .

Una libreria chiama il TryGetSwitch metodo per verificare se i propri consumer hanno dichiarato il valore dell'opzione e quindi agire in modo appropriato su di esso. Per impostazione predefinita, se l'opzione non è definita, la nuova funzionalità è abilitata. Se l'opzione è definita e il relativo valore è false, è abilitata anche la nuova funzionalità. Se il relativo valore è true, il comportamento legacy è abilitato.

Si applica a

Vedi anche