BooleanSwitch 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 BooleanSwitch 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| BooleanSwitch(String, String) |
初始化一個以指定顯示名稱與描述的新類別實例 BooleanSwitch 。 |
| BooleanSwitch(String, String, String) |
初始化一個新的類別實例 BooleanSwitch ,並以指定的顯示名稱、描述及預設交換器值。 |
BooleanSwitch(String, String)
初始化一個以指定顯示名稱與描述的新類別實例 BooleanSwitch 。
public:
BooleanSwitch(System::String ^ displayName, System::String ^ description);
public BooleanSwitch(string displayName, string? description);
public BooleanSwitch(string displayName, string description);
new System.Diagnostics.BooleanSwitch : string * string -> System.Diagnostics.BooleanSwitch
Public Sub New (displayName As String, description As String)
參數
- displayName
- String
名稱將顯示在使用者介面上。
- description
- String
交換器的描述。
範例
以下範例建立 並 BooleanSwitch 利用開關判斷是否要列印錯誤訊息。 參數會在類別層級建立。 該 Main 方法將其位置傳給 MyMethod,該會印出錯誤訊息及錯誤發生的位置。
// 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");
}
' Class level declaration.
' Create a BooleanSwitch for data.
Private Shared dataSwitch As New BooleanSwitch("Data", "DataAccess module")
Public Shared Sub MyMethod(location As String)
' Insert code here to handle processing.
If dataSwitch.Enabled Then
Console.WriteLine(("Error happened at " + location))
End If
End Sub
' Entry point which delegates to C-style main function.
Public Overloads Shared Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Public Shared Sub Main(args() As String)
' Run the method which writes an error message specifying the location of the error.
MyMethod("in Main")
End Sub
備註
當你建立 BooleanSwitch 時,displayName 參數會用來在應用程式設定檔中找到 .NET Framework 應用程式的初始切換設定。 如果建構子找不到初始設定,或對於 .NET Core 和 .NET 5+ 應用程式,Enabled 屬性會被設定為 false(停用)。
要在 .NET Framework 應用程式中設定你的 BooleanSwitch 等級,請編輯與應用程式名稱對應的設定檔。 在這個檔案中,你可以新增一個開關並設定其值、移除開關,或清除應用程式先前設定的所有開關。 設定檔格式應如以下範例:
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="10" />
<add name="myNewSwitch" value="20" />
<remove name="mySwitch" />
<clear/>
</switches>
</system.diagnostics>
</configuration>
Note
你建立的開關應該是 static。
另請參閱
適用於
BooleanSwitch(String, String, String)
初始化一個新的類別實例 BooleanSwitch ,並以指定的顯示名稱、描述及預設交換器值。
public:
BooleanSwitch(System::String ^ displayName, System::String ^ description, System::String ^ defaultSwitchValue);
public BooleanSwitch(string displayName, string? description, string defaultSwitchValue);
public BooleanSwitch(string displayName, string description, string defaultSwitchValue);
new System.Diagnostics.BooleanSwitch : string * string * string -> System.Diagnostics.BooleanSwitch
Public Sub New (displayName As String, description As String, defaultSwitchValue As String)
參數
- displayName
- String
使用者介面上要顯示的名稱。
- description
- String
交換器的描述。
- defaultSwitchValue
- String
開關的預設值。