AppContext.SetSwitch(String, Boolean) AppContext.SetSwitch(String, Boolean) AppContext.SetSwitch(String, Boolean) AppContext.SetSwitch(String, Boolean) Method

定義

設定參數的值。Sets the value of a switch.

public:
 static void SetSwitch(System::String ^ switchName, bool isEnabled);
public static void SetSwitch (string switchName, bool isEnabled);
static member SetSwitch : string * bool -> unit
Public Shared Sub SetSwitch (switchName As String, isEnabled As Boolean)

參數

switchName
String String String String

參數的名稱。The name of the switch.

isEnabled
Boolean Boolean Boolean Boolean

參數的值。The value of the switch.

例外狀況

範例

下列程式碼會設定名為的交換器Switch.AmazingLib.ThrowOnExceptiontrue,可使用舊版的行為。The following line of code sets a switch named Switch.AmazingLib.ThrowOnException to true, which enables a legacy behavior. 程式庫接著可以選取程式庫取用者是否已設定參數值呼叫TryGetSwitch方法。The library can then check whether a library consumer has set the value of the switch by calling the TryGetSwitch method.

AppContext.SetSwitch("Switch.AmazingLib.ThrowOnException", true);
AppContext.SetSwitch("Switch.AmazingLib.ThrowOnException", True)

備註

AppContext類別可讓您的程式庫作者為使用者提供的新功能的統一退出機制。The AppContext class enables library writers to provide a uniform opt-out mechanism for new functionality for their users. 它會建立元件之間的鬆散結合合約,以便溝通退出要求。It establishes a loosely-coupled contract between components in order to communicate an opt-out request. 變更現有的功能時,此功能通常特別重要。This capability is typically important when a change is made to existing functionality. 相反地,已經有新功能的隱含選擇加入。Conversely, there is already an implicit opt-in for new functionality.

SetSwitch方法會呼叫應用程式 (或程式庫) 來宣告參數的值 (一律為Boolean值),相依程式庫所定義。The SetSwitch method is called by an application (or a library) to declare the value of a switch (which is always a Boolean value) that a dependent library defines. 參數是一律以隱含方式false,以提供新的行為。The switch is always implicitly false, which provides the new behavior. 將參數設定為true啟用它,它提供舊版的行為。Setting the switch to true enables it, which provides the legacy behavior. 明確地將參數設定為false也提供新的行為。Explicitly setting the switch to false also provides the new behavior. 相依程式庫可以再檢查參數值呼叫TryGetSwitch方法。The dependent library can then check the value of the switch by calling the TryGetSwitch method.

注意

很有幫助的交換器名稱,使用一致的格式,因為它們是程式庫所公開的正式合約。It's beneficial to use a consistent format for switch names, since they are a formal contract exposed by a library. 以下是兩種明顯的格式。The following are two obvious formats.

  • 參數.命名空間.參數名稱Switch.namespace.switchname
  • 參數.程式庫.參數名稱Switch.library.switchname

在.NET Framework 中,除了以程式設計方式設定參數的值上執行的應用程式它也可以設定:For applications running on the .NET Framework, in addition to setting the value of a switch programmatically, it can also be set:

  • 藉由新增的參數名稱和值 <Appcontextswitchoverrides> >中的項目<執行階段 >應用程式組態檔區段。By adding the switch name and value to the <AppContextSwitchOverrides> element in the <runtime> section of an application configuration file. 例如,下列範例會定義名為的交換器Libraries.FPLibrary.UseExactFloatingPointComparison增值是FalseFor example, the following defines a switch named Libraries.FPLibrary.UseExactFloatingPointComparison whose value is False.

    
    <configuration>
       <runtime>
          <AppContextSwitchOverrides value="Libraries.FPLibrary.UseExactFloatingPointComparison=false" />
       </runtime>
    </configuration>
    
  • 藉由新增其名稱是參數名稱的字串值以HKLM\SOFTWARE\Microsoft\.NETFramework\AppContext金鑰在登錄中。By adding a string value whose name is the name of the switch to the HKLM\SOFTWARE\Microsoft\.NETFramework\AppContext key in the registry. 其值必須是字串表示法Boolean,就能剖析Boolean.Parse方法,也就是它必須是"True","true","False"或"false"。Its value must be the string representation of a Boolean that can be parsed by the Boolean.Parse method; that is, it must be "True", "true", "False", or "false".

如果switchName已經存在,其值會覆寫isEnabled引數。If switchName already exists, its value is overwritten by the isEnabled argument. 也就是最新的呼叫來SetSwitch方法會覆寫登錄,在應用程式組態檔,或由先前呼叫中所定義的值SetSwitch方法。That is, the most recent call to the SetSwitch method overrides the value defined in the registry, in an app configuration file, or by previous calls to the SetSwitch method.

AppContext.SetSwitch 和.NET CoreAppContext.SetSwitch and .NET Core

.NET core 支援使用以程式設計方式呼叫SetSwitch僅方法。.NET Core supports programmatic calls with the SetSwitch method only. 支援下列參數:The following switches are supported:

參數Switch Values 描述Description
System.Net.Http.SocketsHttpHandler.Http2Support truefalsetrue or false 指出是否支援 HTTP/2 通訊協定已啟用 (true) 或停用 (false)。Indicates whether support for the HTTP/2 protocol is enabled (true) or disabled (false). 預設會停用。The default is disabled. 第一次使用之前,必須設定交換器HttpClientThe switch must be set before the first use of HttpClient. 適用於開始使用.NET Core 3.0 Preview 4。Available starting with .NET Core 3.0 Preview 4.
System.Net.Http.UseSocketsHttpHandler truefalsetrue or false 判斷是否有高層級網路 Api,例如HttpClient使用System.Net.Http.SocketsHttpHandler(true) 或System.Net.Http.HttpClientHandler(false)。Determines whether high-level networking APIs such as HttpClient use System.Net.Http.SocketsHttpHandler (true) or System.Net.Http.HttpClientHandler (false).

適用於

另請參閱