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

例外狀況

switchNamenull

switchNameEmpty

範例

下列範例會判斷程式庫取用者是否已設定名為 的 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 可讓程式庫寫入器為其使用者提供一致的退出機制。 其會建立元件之間的鬆散結合合約,以便溝通退出要求。 變更現有的功能時,此功能通常特別重要。 相反地,已經有新功能的隱含選擇加入。

Common Language Runtime 會藉由讀取登錄和應用程式的組態檔,自動填入指派給 AppContext 實例的參數。 然後,您可以藉由呼叫 SetSwitch 方法來覆寫這些參數的值,並新增新的參數。

程式庫會呼叫 TryGetSwitch 方法來檢查其取用者是否已宣告參數的值,然後適當地對其採取行動。 根據預設,如果未定義參數,則會啟用新功能。 如果參數已定義且其值為 false ,則也會啟用新功能。 如果其值為 true ,則會啟用舊版行為。

適用於

另請參閱