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。 此参数未经初始化即被传递。

返回

如果设置了 switchNameisEnabled 参数包含开关的值,则为 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 使库编写器能够为用户的新功能提供统一的选择退出机制。 它在组件之间建立松耦合的协定,以便与选择退出请求进行通信。 对现有功能进行更改时,此功能通常很重要。 相反,已有新功能隐式选择加入。

公共语言运行时通过读取注册表和应用程序的配置文件自动填充分配给 AppContext 实例的开关。 然后,可以通过调用 SetSwitch 方法重写这些开关的值,并添加新开关。

库调用 TryGetSwitch 方法以检查其使用者是否已声明开关的值,然后对其进行适当操作。 默认情况下,如果未定义开关,则会启用新功能。 如果定义了开关,并且其值为 false,则还会启用新功能。 如果其值为 true,则启用旧行为。

适用于

另请参阅