BooleanSwitch.Enabled 屬性

定義

取得或設定值,指出此參數是否已啟用或停用。

C#
public bool Enabled { get; set; }

屬性值

如果已啟用此參數,則為 true;否則為 false。 預設為 false

例外狀況

呼叫端沒有正確的權限。

範例

下列程式代碼範例會 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 that writes an error message specifying the location of the error.
    MyMethod("in Main");
}

備註

根據預設,此欄位會設定為 false (停用) 。 若要啟用參數,請將 的值 true指派給這個欄位。 若要停用 參數,請將值指派給 false。 這個屬性的值取決於基類屬性 SwitchSetting的值。

適用於

產品 版本
.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

另請參閱