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

定义

尝试获取开关的值。Tries to get the value of a switch.

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
Public Shared Function TryGetSwitch (switchName As String, ByRef isEnabled As Boolean) As Boolean

参数

switchName
String String String String

开关的名称。The name of the switch.

isEnabled
Boolean Boolean Boolean Boolean

此方法返回时,如果找到 switchName,则包含 switchName 的值;如果未找到 switchName,则为 falseWhen this method returns, contains the value of switchName if switchName was found, or false if switchName was not found. 此参数未经初始化即被传递。This parameter is passed uninitialized.

返回

如果设置了 switchNameisEnabled 参数包含开关的值,则为 true;否则为 falsetrue if switchName was set and the isEnabled argument contains the value of the switch; otherwise, false.

异常

示例

下面的示例确定库使用者是否已将名为的交换机设置Switch.AmazingLib.ThrowOnExceptionThe following example determines whether a library consumer has set a switch named 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
      }
   }
}
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类使库编写器可为其用户提供统一的新功能选择退出机制。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.

公共语言运行时自动填充分配给的开关AppContext通过读取注册表和应用程序的配置文件的实例。The common language runtime automatically populates the switches assigned to an AppContext instance by reading the registry and the application's configuration file. 然后可以重写这些开关的值,并添加新的开关,通过调用SetSwitch方法。The value of these switches can then be overridden, and new switches added, by calling the SetSwitch method.

一个库调用TryGetSwitch方法来检查其使用者是否已声明该开关的值,然后对其进行适当地处理。A library calls the TryGetSwitch method to check whether its consumers have declared the value of the switch and then act appropriately on it. 默认情况下,如果未定义该交换机,新功能被启用...By default, if the switch is not defined, the new functionality is enabled.. 如果定义了开关,其值为false,也可以启用新功能。If the switch is defined and its value is false, the new functionality is also enabled. 如果其值为true,启用旧行为。If its value is true, the legacy behavior is enabled.

适用于

另请参阅