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

Definition

设置开关的值。 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.

Exceptions

Examples

以下代码行设置名为的交换机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)

Remarks

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.switchnameSwitch.namespace.switchname
    • Switch.library.switchnameSwitch.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.useSocketsHttpHandler true false

Applies to

See Also