AppContext.TryGetSwitch(String, Boolean) Methode

Definitie

Probeert de waarde van een switch op te halen.

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

Parameters

switchName
String

De naam van de schakeloptie.

isEnabled
Boolean

Wanneer deze methode wordt geretourneerd, bevat deze de waarde van switchName of switchName deze is gevonden of false als switchName deze niet is gevonden. Deze parameter wordt niet-geïnitialiseerd doorgegeven.

Retouren

trueals switchName het argument is ingesteld en het isEnabled argument de waarde van de schakeloptie bevat, anders. false

Uitzonderingen

switchName is null.

switchName is Empty.

Voorbeelden

In het volgende voorbeeld wordt bepaald of een bibliotheekgebruiker een switch met de naam Switch.AmazingLib.ThrowOnExceptionheeft ingesteld.

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

Opmerkingen

Met de AppContext-klasse kunnen bibliotheekschrijvers een uniform opt-outmechanisme bieden voor nieuwe functionaliteit voor hun gebruikers. Er wordt een losjes gekoppeld contract tussen componenten tot stand gebracht om een opt-out-aanvraag te communiceren. Deze mogelijkheid is doorgaans belangrijk wanneer een wijziging wordt aangebracht in bestaande functionaliteit. Omgekeerd is er al een impliciete opt-in voor nieuwe functionaliteit.

De algemene taalruntime vult automatisch de switches die zijn toegewezen aan een AppContext exemplaar door het register en het configuratiebestand van de toepassing te lezen. De waarde van deze switches kan vervolgens worden overschreven en nieuwe switches worden toegevoegd door de methode aan te SetSwitch roepen.

Een bibliotheek roept de TryGetSwitch methode aan om te controleren of de consumenten de waarde van de switch hebben gedeclareerd en vervolgens op de juiste wijze actie hebben uitgevoerd. Als de switch niet is gedefinieerd, is de nieuwe functionaliteit standaard ingeschakeld. Als de switch is gedefinieerd en de waarde ervan is false, is de nieuwe functionaliteit ook ingeschakeld. Als de waarde is true, is het verouderde gedrag ingeschakeld.

Van toepassing op

Zie ook