AppContext.TryGetSwitch(String, Boolean) 메서드

정의

스위치의 값을 가져오려고 합니다.

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

매개 변수

switchName
String

스위치의 이름입니다.

isEnabled
Boolean

이 메서드가 반환될 때 switchName을 찾은 경우 switchName의 값을 포함하거나, switchName을 찾을 수 없으면 false의 값을 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

switchName이 설정되었으며 isEnabled 인수에 스위치의 값이 포함되어 있으면 true이고, 그러지 않으면 false입니다.

예외

switchName이(가) null인 경우

switchName이(가) Empty인 경우

예제

다음 예제에서는 라이브러리 소비자가 라는 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

설명

AppContext 이 클래스를 사용하면 라이브러리 작성기가 사용자에게 새 기능을 위한 균일한 옵트아웃 메커니즘을 제공할 수 있습니다. 옵트아웃(opt out) 요청을 전달하기 위해 구성 요소 간에 느슨하게 결합된 계약을 설정합니다. 이 기능은 일반적으로 기존 기능이 변경될 때 중요합니다. 반대로, 새로운 기능에 대한 암시적 옵트인(opt in)은 이미 있습니다.

공용 언어 런타임에 할당 하는 스위치를 자동으로 채우려고는 AppContext 레지스트리 및 애플리케이션의 구성 파일을 읽어 인스턴스. 그런 다음 메서드를 호출하여 이러한 스위치의 값을 재정의하고 새 스위치를 SetSwitch 추가할 수 있습니다.

라이브러리는 메서드를 TryGetSwitch 호출하여 소비자가 스위치의 값을 선언했는지 여부를 검사 다음 적절하게 작동합니다. 기본적으로 스위치가 정의되지 않은 경우 새 기능이 사용하도록 설정됩니다. 스위치가 정의되고 해당 값이 false이면 새 기능도 사용하도록 설정됩니다. 값이 이 true면 레거시 동작이 활성화됩니다.

적용 대상

추가 정보