BooleanSwitch 构造函数

定义

初始化 BooleanSwitch 类的新实例。

重载

BooleanSwitch(String, String)

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

BooleanSwitch(String, String, String)

使用指定的显示名称、描述和默认切换值初始化 BooleanSwitch 类的新实例。

BooleanSwitch(String, String)

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,这会输出错误消息以及发生错误的位置。

public ref class BooleanSwitchTest
{
private:

   /* Create a BooleanSwitch for data.*/
   static BooleanSwitch^ dataSwitch = gcnew BooleanSwitch( "Data","DataAccess module" );

public:
   static void MyMethod( String^ location )
   {
      
      //Insert code here to handle processing.
      if ( dataSwitch->Enabled )
            Console::WriteLine( "Error happened at {0}", location );
   }

};

int main()
{
   
   //Run the method which writes an error message specifying the location of the error.
   BooleanSwitchTest::MyMethod( "in main" );
}
// 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

使用指定的显示名称、描述和默认切换值初始化 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

切换的默认值。

适用于