语言

BooleanSwitch 构造函数

定义

初始化 BooleanSwitch 类的新实例。

重载

名称 说明
BooleanSwitch(String, String)

使用指定的显示名称和说明初始化类的新实例 BooleanSwitch 。

BooleanSwitch(String, String, String)

使用指定的显示名称、说明和默认开关值初始化类的新实例 BooleanSwitch 。

BooleanSwitch(String, String)

Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs

使用指定的显示名称和说明初始化类的新实例 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>

注释

创建的开关应为 static。

另请参阅

适用于

BooleanSwitch(String, String, String)

Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs

使用指定的显示名称、说明和默认开关值初始化类的新实例 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

开关的默认值。

适用于