BooleanSwitch 建構函式

定義

初始化 BooleanSwitch 類別的新執行個體。

多載

BooleanSwitch(String, String)

使用指定的顯示名稱和描述,初始化 BooleanSwitch 類別的新執行個體。

BooleanSwitch(String, String, String)

以指定的顯示名稱、描述和預設參數值初始化 BooleanSwitch 類別的新執行個體。

BooleanSwitch(String, String)

來源:
BooleanSwitch.cs
來源:
BooleanSwitch.cs
來源:
BooleanSwitch.cs

使用指定的顯示名稱和描述,初始化 BooleanSwitch 類別的新執行個體。

C#
public BooleanSwitch (string displayName, string? description);
C#
public BooleanSwitch (string displayName, string description);

參數

displayName
String

要顯示在使用者介面上的名稱。

description
String

參數的描述。

範例

下列範例會 BooleanSwitch 建立 ,並使用 參數來判斷是否要列印錯誤訊息。 參數會在類別層級建立。 方法 Main 會將其位置傳遞至 MyMethod,這會列印錯誤訊息,以及發生錯誤的位置。

C#
// Class level declaration.
/* Create a BooleanSwitch for data.*/
static BooleanSwitch dataSwitch = new BooleanSwitch("Data", "DataAccess module");

static public void MyMethod(string location)
{
    //Insert code here to handle processing.
    if (dataSwitch.Enabled)
        Console.WriteLine("Error happened at " + location);
}

public static void Main(string[] args)
{
    //Run the method which writes an error message specifying the location of the error.
    MyMethod("in Main");
}

備註

當您建立 BooleanSwitch時,displayName參數會用來尋找應用程式組態檔中 .NET Framework 應用程式的初始參數設定。 如果建構函式找不到初始設定,或 .NET Core 和 .NET 5+ 應用程式,屬性 Enabled 會設定為 false (停用) 。

若要在 .NET Framework 應用程式中設定層級BooleanSwitch,請編輯對應至應用程式名稱的組態檔。 在此檔案中,您可以新增參數並設定其值、移除參數,或清除應用程式先前設定的所有參數。 組態檔的格式應該如下列範例所示:

XML
<configuration>  
    <system.diagnostics>  
       <switches>  
          <add name="mySwitch" value="10" />  
          <add name="myNewSwitch" value="20" />  
          <remove name="mySwitch" />  
          <clear/>  
       </switches>  
    </system.diagnostics>  
 </configuration>  

備註

您建立的參數應該是 static

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

BooleanSwitch(String, String, String)

來源:
BooleanSwitch.cs
來源:
BooleanSwitch.cs
來源:
BooleanSwitch.cs

以指定的顯示名稱、描述和預設參數值初始化 BooleanSwitch 類別的新執行個體。

C#
public BooleanSwitch (string displayName, string? description, string defaultSwitchValue);
C#
public BooleanSwitch (string displayName, string description, string defaultSwitchValue);

參數

displayName
String

要在使用者介面上顯示的名稱。

description
String

參數的描述。

defaultSwitchValue
String

參數的預設值。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1